Forth vs Lisp
c2.com
c2.com
Never tried Forth? Do yourself a favor and start now with jsforth[1] or install the "gforth" package. Read Starting Forth[2] for a gentle introduction to the language and Thinking Forth[3] for an in-depth examination of the idioms and patterns of Forth programming.
[1] http://forthfreak.net/jsforth80x25.html
I think that sociologically Lisp and Forth users are more alike than not, but the camps rarely seem talk to each other. It's clear from Forth's history that it has a secure niche in 8- & 16-bit microcontroller applications. I have no idea if there is such a thing as a Lisp application niche these days.
[1] http://news.ycombinator.com/item?id=2242594
Edit: link and typo
// In the early 90's I got a job that needed me to create a lot of reports / graphs out of a foxbase database. I had a postscript printer and a copy of Turbo C 2.0 (don't forget to download the floating point patch). Gotta love hand coding Postscript templates. I was loving life when I finally got a openstep box.
3 5 > if ." greater" else ." less" then
PostScript goes with operators like "if" and "ifelse" that consume code literals from the stack (enclosed in curly braces): 3 5 gt { (greater) print } { (less) print } ifelse
It's a little heavier weight, but very flexible and it feels like a natural way to handle postfix conditionals.As I understand it, Forth shines when you look at the entirety of a project and are prepared to find the leanest, most elegant solution to the exact problem, not limited to writing programs for commodity hardware. Forth people are kind of like Motie Engineers[1].
[1] http://en.wikipedia.org/wiki/The_Mote_in_God%27s_Eye#Moties
http://formlis.wordpress.com/2010/06/30/forth-in-lisp/
Anybody know of vice versa?
Let’s use “(* 4 (+ 5 6))” as an example input string.
1. Drop ( and ): “* 4 + 5 6”.
2. Tokenize on whitespace and reverse the tokens: 6 5 + 4 *
3. This is forth. Execute it.
There’s at least one class of bug here (that “(- a b)” != “b a -”)), but it shows a sense in which they’re very similar languages. From 40,000 feet, lisp could be called a syntax hack to add variadic functions to a backwards dialect of forth.
To put it another way, forth really is trivially tree-structured just like lisp, but the trees are implicit in the number of arguments the functions take. This allows tricks that depend on the AST being easy to work with, just like lisp macros.
- define [+] something like:
: [+] 0 swap 0 do + loop ;
- define (+ as a word that pushes the address of [+] and the stack pointer onto the retun stack
- similarly define [-] and (- , [* ] and (* , and [/] and (/
- define ) as a word that takes the current stack pointer, pops that saved stack pointer from the return stack and subtracts it from it (this puts the number of arguments to [+] on the stack, pops the address of [+] from the return stack and calls it.
With that, "(* 4 (+ 5 6 ) )" becomes executable forth.
Was very tempted to say "... match between two dead languages that ..."
But then somebody would nitpick, and they'd get 500 karma, and I'd get -20 and I would burn with the fury of a thousand suns... or if not that then at least the mild annoyance of a social worker watching a hobo be set on fire but not being arsed to do anything about it.
/sarcasm