Level 0, lesson 1

# 0.1 Making calculations without parenthesis

To use 4tH you must understand Reverse Polish Notation. This is a way to write arithmetic expressions. The form is a bit tricky for people to understand, since it is geared towards making it easy for the computer to perform calculations; however, most people can get used to the notation with a bit of practice.

Reverse Polish Notation stores values in a stack. A stack of values is just like a stack of books: one value is placed on top of another. When you want to perform a calculation, the calculation uses the top numbers on the stack. For example, here's a typical addition operation:

         1 2 +

When 4tH reads a number, it just puts the value onto the stack. Thus 1 goes on the stack, then 2 goes on the stack. When you put a value onto the stack, we say that you push it onto the stack. When 4tH reads the operator '+', it takes the top two values off the stack, adds them, then pushes the result back onto the stack. This means that the stack contains:

         3

after the above addition. As another example, consider:

         2 3 4 + *

(The '*' stands for multiplication.) 4tH begins by pushing the three numbers onto the stack. When it finds the '+', it takes the top two numbers off the stack and adds them. (Taking a value off the stack is called popping the stack.) 4tH then pushes the result of the addition back onto the stack in place of the two numbers. Thus the stack contains:

         2 7

When 4tH finds the '*' operator, it again pops the top two values off the stack. It multiplies them, then pushes the result back onto the tack, leaving:

         14

The following table gives a few more examples of Reverse Polish expressions. After each, we show the contents of the stack.

Calculation | Result
--- | ---
7 2 - | 5
2 7 - | -5
12 3 / | 4
-12 3 / | -4
4 5 + 2 * | 18
4 5 2 + * | 28
4 5 2 * - | -6