Anyone that's been following me over the past week or so knows that I've been trying to stumble my way around a fairly new programming language Factor. I've already done a few simple walk-through posts and talked a little bit about why I'm interested in Factor in the first place. But today I thought I would take a step back and talk about a basic concept in Factor ... words.
Basically words are what make up your Factor programs (duh) and in many other languages you might think of them as functions or methods. They are the things that you will be calling or executing (however you want to think of it) throughout your programs.
You can define words - for example:
: stupid ( x -- x )
write " is stupid." print ;
And then use your words any time after they have been defined -- for example:
"This example" stupid
Note: The ( x -- x ) bit from our word definition is intended to show the stack effect of our word. However this is for documentation purposes only and has no real effect on what we can (or do) actually make our word accomplish. Technically you can omit this from your word declarations, but it's good form to include it (and makes your code A LOT easier to read, use, and debug for the rest of us).
Best of all, and as you probably guessed by now, Factor comes with a whole slew of words already defined for you in gobs of vocabularies (those are the .factor files you see filling up the core, extra, and misc folders). To be able to use any of the words in any of those vocabularies you just need to include the .factor file in your USING clause -- for example:
USING: io ;
This would give you access to use any words defined in the /core/io/io.factor file.
All of this really means that learning Factor is really all about learning words (and grasping the whole stack concept, but that's a whole different post!). As I mentioned, there are already lots and lots of words you can learn. So to save my fellow newbies just a little time, I'm going to outline some of the more interesting words you should probably learn first (in no special order).
Before we get started with the details and examples of each word, let's put a few things on the stack -- remember that words generally do things to items on the stack. So our examples will all start with a stack like such:
"Kevin" "learns" "Factor" "slowly"
Now we can look at some cool words. For each, I'll give the word and it's stack effect definition (as per it's documentation). Then I'll show the result of the stack if we had executed the word on our example stack (shown above)
over ( x y -- y x )
"Kevin" "learns" "Factor" "slowly" "Factor"
dup ( x -- x x )
"Kevin" "learns" "Factor" "slowly" "slowly"
swap ( x y -- y x )
"Kevin" "learns" "slowly" "Factor"
nip ( x y -- y )
"Kevin" "learns" "slowly"
NOTE: How the heck did they know that?!
pick ( x y z -- x y z x )
"Kevin" "learns" "Factor" "slowly" "learns"
rot ( x y z -- y z x )
"Kevin" "Factor" "slowly" "learns"
-rot ( x y z -- z x y )
"Kevin" "slowly" "learns" "Factor"
roll ( w x y z -- x y z w )
"learns" "Factor" "slowly" "Kevin"
keep ( x quot -- x )
Here I need to actually provide a quote as well, so for this example we'll call [ print ] keep which will print the word "slowly" and the stack will be:
"Kevin" "learns" "Factor" "slowly"
>r ( x -- )
This is a special word that actually moves the top item on the stack to the retain stack. It's removed from the working stack, but it's not permanently (or magically) lost to the programming gods (as we'll show with the next word). The stack after we use this word now looks like this:
"Kevin" "learns" "Factor"
r> ( -- x )
The reverse of the special >r word I just explained. As you can probably guess, this pulls the first item off the top of the retain stack and puts it on the top of the working stack. So assuming we called this directly after we had called the >r word (we'll get a retain stack error if the retain stack is empty so we can't use our usual stack starting point that we did for the other examples listed) our working stack will now look like this:
"Kevin" "learns" "Factor" "slowly"
set ( value variable -- )
This word lets you define a variable and set it's value. For example let's say we want to assign the value "cool" to the variable "whatsfactor" (this does not store anything on the stack but rather sets a variable in memory):
"cool" "whatsfactor" set
get ( variable -- value )
Once we've got a variable set using the 'set' word we just talked about, we can access that variable's value with the get call. For our example:
"whatsfactor" get
will put "cool" on the top of our stack.
So there you have it, some of the basic Factor words and concepts. It probably doesn't seem like all that much, but believe me there are a ton of really cool things you can do with just this small set of words. And there's plenty more words where they came from, just check out the Factor Documentation if you don't believe me!
P.S. This post may seem a little bluish to some people...that's because the majority of these 'core' words a newbie must know was provided to me by none other than the famous (or is it infamous?) Chris Double. Once you've moved beyond my feeble posts, you should check his more advanced (and accurate) information out!



