Playing with the stack

Factor handbook ยป Guided tour of Factor

Factor handbook ยป Guided tour of Factor

Prev: | Concatenative Languages |

Next: | Defining our first word |

Let us start looking what Factor actually feels like. Our first words will be literals, like 3, 12.58 or "Chuck Norris". Literals can be thought as functions that push themselves on the stack. Try writing 5 in the listener and then press enter to confirm. You will see that the stack, initially empty, now looks like

You can enter more than one number, separated by spaces, like 7 3 1, and get

(the interface shows the top of the stack on the bottom). What about operations? If you write +, you will run the + function, which pops the two topmost elements and pushes their sum, leaving us with

You can put additional inputs in a single line, so for instance - * will leave the single number 15 on the stack (do you see why?).

You may end up pushing many values to the stack, or end up with an incorrect result. You can then clear the stack with the keystroke Alt+Shift+K on Linux/Windows or Cmd+Shift+K on MacOS.

The function . (a period or a dot) prints the item at the top of the stack, while popping it out of the stack, leaving the stack empty.

If we write everything on one line, our program so far looks like

which shows Factor's peculiar way of doing arithmetic by putting the arguments first and the operator last - a convention which is called Reverse Polish Notation (RPN). Notice that RPN requires no parenthesis, unlike the polish notation of Lisps where the operator comes first, and RPN requires no precedence rules, unlike the infix notation used in most programming languages and in everyday arithmetic. For instance in any Lisp, the same computation would be written as

and in familiar infix notation

Also notice that we have been able to split our computation onto many lines or combine it onto fewer lines rather arbitrarily, and that each line made sense in itself.

5

You can enter more than one number, separated by spaces, like 7 3 1, and get

5
7
3
1

(the interface shows the top of the stack on the bottom). What about operations? If you write +, you will run the + function, which pops the two topmost elements and pushes their sum, leaving us with

5
7
4

You can put additional inputs in a single line, so for instance - * will leave the single number 15 on the stack (do you see why?).

You may end up pushing many values to the stack, or end up with an incorrect result. You can then clear the stack with the keystroke Alt+Shift+K on Linux/Windows or Cmd+Shift+K on MacOS.

The function . (a period or a dot) prints the item at the top of the stack, while popping it out of the stack, leaving the stack empty.

If we write everything on one line, our program so far looks like

5 7 3 1 + - * .

which shows Factor's peculiar way of doing arithmetic by putting the arguments first and the operator last - a convention which is called Reverse Polish Notation (RPN). Notice that RPN requires no parenthesis, unlike the polish notation of Lisps where the operator comes first, and RPN requires no precedence rules, unlike the infix notation used in most programming languages and in everyday arithmetic. For instance in any Lisp, the same computation would be written as

(* 5 (- 7 (+ 3 1)))

and in familiar infix notation

(7 - (3 + 1)) * 5

Also notice that we have been able to split our computation onto many lines or combine it onto fewer lines rather arbitrarily, and that each line made sense in itself.

This documentation was generated offline from a
`load-all`

image. If you want, you can also
browse the documentation from within the UI developer tools. See
the Factor website
for more information.

Factor 0.99 x86.64 (2190, heads/master-46c625f8dc, Feb 4 2023 19:37:27)