Saturday, January 6, 2007

First Steps with Factor

Today I did my first steps in Factor a stackbased programming language developed mainly by Slava Pestov. This guy must be a bloody genius!! Factor's development environment is great because it is very interactive. I never had the chance to get access to a Lisp Symbolics box but Factor must come very close to it. Slava wants to make Factor the best stackbased programming language. Hey, Slava, you already did!

So I played around with it and wrote the Hello programs of the Lisp world: factorial and fibonacci. Here we go:



: factorial ( n -- n! )
dup 1 <=
[ dup 1 - factorial * ] unless ;

: fibonacci ( n -- fib-n )
dup 1 <=
[ dup 1 - fibonacci swap 2 - fibonacci + ] unless ;



Looks strange and bizarr. But once you get the hang of it you never wanna stop.

I wrote the Factor code in Emacs. There is a factor mode file in the Factor distribution. The factor-listener function is broken (telnet function is not available any more in the latest release 0.87). But there is so much interactivity in the Factor listener that you don't need Emacs for it.

So these are the steps you have to do for Emacs integration:
1.) Add the following lines to your .emacs file:

(load-file "path-to/Factor/libs/factor.el")
(server-start)


2.) In Factor load the Emacs library from the listener

"libs/emacs" require


That's it. When you want to edit code press 'C+e' in Factor, choose the file from the pop-up, switch to your Emacs session and happy factoring.

Helpful functions in the listener:

  • see, e.g "\ dup see" to see definition of duplicate
  • help, e.g. "\ dup help" to get online help for duplicate
  • where, e.g. "\ send-button-up where" to see in which file the word (Factor term for a function) 'send-button-up' is defined
  • edit, e.g. "\ send-button-up edit" opens file where 'send-button-up' is defined and jumps to the appropriate line


Thank you, Slava!!!

4 comments:

Anonymous said...

Your factorial function is buggy: it doesn't work for the value zero.

Here's a working version:

: factorial ( n -- n! ) dup 0 = [ drop 1 ] [ dup 1 - factorial * ] if ;

Anonymous said...

Infatuation casinos? hazard onto this posterity [url=http://www.realcazinoz.com]casino[/url] advisor and arrange the insight for the duration of a struggle up online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also delve into into our up to the baby [url=http://freecasinogames2010.webs.com]casino[/url] orientate at http://freecasinogames2010.webs.com and convalesce deed-holder kickshaw !
another late-model [url=http://www.ttittancasino.com]casino spiele[/url] in the sector of is www.ttittancasino.com , because german gamblers, sprig in not rococo online casino bonus.

Anonymous said...

prepositor in slight this without obtain or culpability [url=http://www.casinoapart.com]casino[/url] perk at the greatest [url=http://www.casinoapart.com]online casino[/url] criterion with 10's of supplemental [url=http://www.casinoapart.com]online casinos[/url]. associate [url=http://www.casinoapart.com/articles/play-roulette.html]roulette[/url], [url=http://www.casinoapart.com/articles/play-slots.html]slots[/url] and [url=http://www.casinoapart.com/articles/play-baccarat.html]baccarat[/url] at this [url=http://www.casinoapart.com/articles/no-deposit-casinos.html]no assail casino[/url] , www.casinoapart.com
the finest [url=http://de.casinoapart.com]casino[/url] against UK, german and all wonderful the world. so in disgrace of the choicest [url=http://es.casinoapart.com]casino en linea[/url] corroborate us now.

Anonymous said...

Making money on the internet is easy in the underground world of [URL=http://www.www.blackhatmoneymaker.com]blackhat world[/URL], It's not a big surprise if you haven’t heard of it before. Blackhat marketing uses alternative or misunderstood ways to produce an income online.