So Factor is a programming language that belongs to the family of concatenative languages, it’s also a stack-based language. The ancestor to these families of languages is FORTH. However Factor distinguishes itself from FORTH on several points. It is among other things dynamically typed and has a garbage collector.
The installation of Factor is easy (there exists binaries for Windows, Mac OS X and Linux to download at the Factor homepage) and you will get a REPL where you can play around with the language in an easy way. I installed the stable release 0.96 of Factor.
Time for some very simple Factor code.
The result of the expression will be 300 and it shows clearly the stack-based property of the language. Lets walk through the line of code.
- We start by pushing the literal 10 on the stack
- And then we push the literal 5 on the stack
- Next is the word +, which will be executed by the runtime. The word + takes two inputs and these inputs will be popped from the stack, 5 and 10 in this case. The result, 15, will pushed on the stack
- Then the literal 20 will be pushed on the stack
- Now there is another word, *, that will be executed. This word will also pop two inputs from the stack. The stack has the literals 20 and 15 and therefore the result will be 300 and pushed on the stack
- Last the word . will be executed. It will pop one item from the stack, 300, as its input and then print it in the listener’s output area.
Lets try to create a new word in Factor.
Now we will have a new word that we can use like this;
The result of the expression above will be 8. I will now try to explain the definition of the word timesTwo.
- First we start the line with : (the colon), which says that we are starting a word definition
- Then we name our word to timesTwo
- Next is the stack effect declaration, ( x – y ), this declaration tells us that this word will take one input, x, and return one output, y.
- After the stack effect declaration is the expression 2 * and in conjunction with the input paramter, x, it will be x times 2
- And at last we will end our word definition with ;
To learn more about Factor take a look at this cookbook, I have only touched the basic stuff here. Hopefully there will be some more time for a deeper dive into the sea of Factor and concatenative languages.
There is also a g+ group, Concatenative Programming, that might be worth checking out.