I'm James Hague, a recovering programmer who has been designing video games since the 1980s. This is Why You Spent All that Time Learning to Program and The Pure Tech Side is the Dark Side are good places to start.
Where are the comments?
Understanding What It's Like to Program in Forth
I write Forth code every day. It is a joy to write a few simple words and solve a problem. As brain exercise it far surpasses cards, crosswords or Sudoku
--Chuck Moore, creator of Forth
I've used and enjoyed Forth quite a bit over the years, though I rarely find myself programming in it these days. Among other projects, I've written several standalone tools in Forth, used it for exploratory programming, wrote a Forth-like language for handling data assets for a commercial project, and wrote two standalone 6502 cross assemblers using the same principles as Forth assemblers.
It's easy to show how beautiful Forth can be. The classic example is:
There's also Leo Brodie's oft-cited washing machine program. But as pretty as these code snippets are, they're the easy, meaningless examples, much like the two-line quicksort in Haskell. They're trotted out to show the the strengths of a language, then reiterated by new converts. The primary reason I wrote the Purely Functional Retrogames series, is because of the disconnect between advocates saying everything is easy without destructive updates, and the utter lack of examples of how to approach many kinds of problems in a purely functional way. The same small set of pretty examples isn't enough to understand what it's like to program in a particular language or style.
Chuck Moore's Sudoku quote above is one of the most accurate characterizations of Forth that I've seen. Once you truly understand it, you'll better see what's fun about the language, and also why it isn't as commonly used. What I'd like to do is to start with a trivially simple problem, one that's completely straightforward, even simpler than the infamous FizzBuzz:
Write a Forth word to add together two integer vectors (a.k.a. arrays) of three elements each.
The C version, without bothering to invent custom data types, requires no thought:
In Erlang it's:
In APL and J the solution is a single character:
First Forth Attempt
So now, Forth. We start with a name and stack picture:
Getting the first value out of v1 is easy enough:
rot" brings v1 to the top, then we grab the first element of the array (remember that we need to keep v1 around, hence the
dup). Hmmm...now we've got four items on the stack:
"a" is what I'm calling the first element of v1, using the same letters as in the Erlang function. There's no way to get v2 to the top of the stack, save the deprecated word
pick, so we're stuck.
Second Forth Attempt
Thinking about this a bit more, the problem is we have too many items being dealt with at once, too many items on the stack. v3 sitting there on top is getting in the way, so what if we moved it somewhere else for a while? The return stack is the standard location for a temporary value, so let's try it:
Now that works. We get v3 out of the way, fetch v1 and v2 (keeping them around for later use), then bring back v3 and store the result. Well, almost, because now v3 is gone and we can't use it for the second and third elements.
Third Forth Attempt
This isn't as bad as it sounds. We can just keep v3 over on the return stack for the whole function. Here's an attempt at the full version of
cell+ is roughly the same as
++ in C. "
2 cells +" is equivalent to "
cell+ cell+". Notice how v3 stays on the return stack for most of the function, being fetched with
r@. The "
drop drop" at the end is to get rid of v1 and v2. Some nicer formatting helps show the symmetry of this word:
This can be made more obvious by defining some vector access words:
A little bit of extra verbosity removes one quirk in the pattern:
And that's it--three element vector addition in Forth. One solution at least; I can think of several completely different approaches, and I don't claim that this is the most concise of them. It has some interesting properties, not the least of which is that there aren't any named variables. On the other hand, all of this puzzling, all this revision...to solve a problem which takes no thought at all in most languages. And while the C version can be switched from integers to floating point values just by changing the parameter types, that change would require completely rewriting the Forth code, because there's a separate floating point stack.
Still, it was enjoyable to work this out. Better than Sudoku? Yes.