my tech blog

To iterate is human, to recurse divine

One Evening With Factor

Thanks to this blog post, Why Concatenative Programming Matters, I just had to spend some more time to explore the topic. Therefore I spent an evening with the programming language Factor.

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.

Other concatenative languages are, Joy, Cat and PostScript. There are of course more languages that falls into this category, however these are just examples.

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.

A simple expression in Factor
1
10 5 + 20 * .

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.

  1. We start by pushing the literal 10 on the stack
  2. And then we push the literal 5 on the stack
  3. 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
  4. Then the literal 20 will be pushed on the stack
  5. 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
  6. 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.

A word definition in Factor
1
: timesTwo ( x -- y ) 2 * ;

Now we will have a new word that we can use like this;

Test our new word
1
4 timesTwo .

The result of the expression above will be 8. I will now try to explain the definition of the word timesTwo.

  1. First we start the line with : (the colon), which says that we are starting a word definition
  2. Then we name our word to timesTwo
  3. 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.
  4. After the stack effect declaration is the expression 2 * and in conjunction with the input paramter, x, it will be x times 2
  5. 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.

Comments