Last updated 2/18/08, first updated 2/18/08, so don't expect much yet.

Welcome to Learn Factor

I recently came across the factor language and find it a fascinating language and want to learn more about it. In seeking to learn factor I have stumbled across various documentation sources but none of them are really for beginners seeking to learn the language. Yes, things are documented but they are scattered and are more like reference materials. There is no natural flow or progression to learning the language step-by-step.

The goal of this site is to provide newbies like myself a step-by-step tutorial to learn the language. As of the time of this writing I have only a basic understanding of the language and am having to piece together an understanding from various sources, talking to people, and scouring the Internet. As I learn the language I intend to teach others what I have learned. The hope is others will have an easier time learning the language and can do so much faster.

I'm going to assume the reader has some background working with imperative languages like C, Pascal, Java, C#, VB, or Javascript but are not familiar with functional languages. The reason for doing so is that I think most programmers coming across this page have a significant amount of experience with the former but not with the later.

What is Factor?

To copy the Factor FAQ, Factor is a functional, dynamically-typed, object-oriented, stack-based programming language designed by Slava Pestov. It's sort of like a combination of Forth and Lisp.

An Example

Some of you may immediately understand the meaning of the above statement but to others that doesn't help much. In that case, the best way to explain it is to provide a simple example. Here is an example of some basic Factor code:

100 4 / 2 * 5 +

Users of reverse polish notation (RPN) calculators, like the HP48, will immediately recognize what's going on here. In a more traditional programming language like C you would express the same statement as:

(100 / 4) * 2 + 5

Two terms you need to know are literals and words. A literal is a fundamental type of the language. Examples of literals from above are 100, 4, 2, and 5. The words are /, *, and +. When Factor comes across a literal it will push it onto the stack. A word can be thought of like a function in other languages. A word usually manipulates the stack but it doesn't have to.

The parser reads input left to right and splits the input into tokens, delimited by white space. The white space is mandatory. The following input will not work as you might expect.

100 4/2*5+

The above consists of 2 tokens 100 and 4/2*5+. 100 is considered a literal, while 4/2*5+ is considered a word. One of the differences in Factor is that words can start and consist of characters not typically allowed in other languages. For example, !doSomething is a perfectly valid word. The same goes for 123foobar and even +*/$!.

The Stack

Just like a stack in the real world, a computer stack works the same way. Think of a stack of trays. On each tray you can write something down. You manipulate the stack by pushing or popping things off of it.

Below is an illustration of what's going on.

Stack Operation
100 4 / 2 * 5 +
100
100
4
25
25
2
50
50
5
55

The parser reads the ... more later. I'm done for the day.