Kevin Nelson Marshall
Other entries:
« What makes a good project?

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?! Cheesy

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!

posted by Kevin Marshall on 2008-01-15 00:00:00+00


Subscribe to my RSS feed »

BotFu feed with RSS reader

BotFu feed by Email


Search All Posts »


Kevin Marshall - Who's That?

I'm just your basic programmer. I can't spell to save my life, I'm not the greatest story teller, and I often ramble on about nothing. This blog showcases all of that!

If you're bored drop me an email at info at falicon.com or view my outdated resume.


Stalk Kevin on »

bit.ly
Delicious
Digg
Disqus
Facebook
Flickr
FriendFeed
Github
Last.fm
LinkedIn
StumbleUpon
Twitter (@falicon)

Archives by Category »

(28) Code »
(8) ColdFusion »
(15) Database »
(10) Factor »
(3) Falcons »
(321) General »
(13) JavaScript »
(18) Perl »
(17) PHP »
(20) Ruby »

Archives by Month »

(1) September 2010 »
(2) August 2010 »
(3) July 2010 »
(13) June 2010 »
(8) May 2010 »
(2) April 2010 »
(2) March 2010 »
(5) January 2010 »
(2) October 2009 »
(6) August 2009 »
(11) July 2009 »
(2) May 2009 »
(3) April 2009 »
(2) March 2009 »
(7) February 2009 »
(9) January 2009 »
(14) December 2008 »
(5) November 2008 »
(12) October 2008 »
(13) September 2008 »
(16) August 2008 »
(23) July 2008 »
(20) June 2008 »
(24) May 2008 »
(23) April 2008 »
(27) March 2008 »
(28) February 2008 »
(26) January 2008 »
(7) December 2007 »

Published Works »

Beginning Amazon's SimpleDB (Apress in dev.)
Pro Active Record (Apress 2007)
Web Services with Rails (O'Reilly 2006).

Contributed To »

Ruby Cookbook (O'Reilly 2006)
SQL Cookbook (O'Reilly 2005)
Various Reviews published in Computing Reviews

Free Code I've Created »

SimpleDB library in Python 3.0



This blog is powered by KickAssCode.