Writing a Forth in Haskell
reinvanderwoerd.nl
reinvanderwoerd.nl
* well you can hack it to give it grouping, but usually it doesn't.
(ANSI) Lisp function calls are safe w.r.t. wrong number of arguments, and support optional and variadic arguments also. This is true of compiled code without source: when we load someone's compiled file and call a function in it with the wrong number of arguments, it is diagnosed.
Forth and Lisp are so miles apart, that I have to scratch my head why a comparison comes up in discussion from time to time.
(It is pretty much always from someone who uses Forth and doesn't know Lisp (or, worse, anything that isn't Forth). "Hey, I heard Lisp is also interactive and meta-programmable, so it must just be a Forth with postfix switched to prefix and parentheses added ...").
(+ 1 2 3)
"(" would pop an pair on a stack, + would set its subroutine, and 1 2 3 would be fed to the subroutine and accumulate a value. when ) is encountered, the data is fed into the subroutine/accumulator pair lower on the stack, so you could still have nesting.Horribly inefficient, but I thought the idea was neat. I'm sure I'm not the first to think of it though.
Point-free can be a very concise and flexible strategy but also error-prone since (in Forth) it allows the stack to leak and consume/return an unbalanced quantity of arguments. This class of error is eliminated within the syntax of Algol style languages languages but can be reproduced easily if you build your own stack machine.
I love Lisp, but what you suggest is not really Forth and will also not give you the performance benefits and simplicity of implementation that Forth has.
I recall reading what the minimal word set needed to be able to write the rest of standard Forth (Fig Forth if I recall) but I seem to remember that most implementations don't push the purity quite that far for performance reasons.
https://rwmj.wordpress.com/2010/08/07/jonesforth-git-reposit...
zForth is yet another Forth, but with some special features not found in most other forths. Note that zForth was written for engineers, not for language purists or Forth aficionados. Its main intention is to be a lightweight scripting language for extending embedded applications on small microprocessors. It is not particularly fast, but should be easy to integrate on any platform with a few kB's of ROM and RAM.
I did exactly this, was curious if I could write an outer and inner interpreter in pure c in under 500 lines with only 5 c functions. I then used that to boot strap an image that can run using only the inner interpreter.
http://chiselapp.com/user/tehologist/repository/compc/index
Also several experimental versions. Final version includes a pdf that documents how the c code works.
has not been mentioned on hn afaik.
How would you describe Forth in relation to those? What makes it stand out and what are its weaknesses?
It is well suited to things like real-time control, you'll probably have a hard time getting used to stack manipulation (especially in the beginning) and it tends to keep you up all night (not sure if that is a strength or a weakness ;) ).
It will also be the most fun you've had with a computer in a long time and it will likely make you look at the rest of what we do with computers as clunky in the extreme.
What Forth is not really suitable for is large projects and things built with a team. It is more of an artisanal thing, something closer to watchmaking or jewelery than major construction.
If you want to know more about why Forth is the way it is you could do worse than to start with studying the life of Chuck Moore for a bit, the language and its author are roughly equally interesting and for want of a better word peculiar.
One thing Forth is not: wasteful.
When I "discovered" Forth, it seemed like science fiction (soo fast!) to me after BASIC juddering around the screen.
Double true if one looks at the hardware and software Chuck Moore makes with it.
Forth is a really cool language, but has the pitfall of trading time for space. It's a very lean language, and incredibly dynamic, but its heavy reliance on jumps makes it run slowly on modern hardware and thrashes most branch predictors.
I would say Forth is easier to use than assembly, and perhaps much more flexible/extensible than C, although in some senses lower level since C gives you a very nice, ordered way of specifying what are the arguments to your functions/etc.
(though your links are slightly broken)
If you search on github there are several clones and indeed forks on JONESFORTH. It is public domain, so I welcome that:
https://github.com/search?utf8=%E2%9C%93&q=jonesforth&type=
There are also versions ported to several other architectures, which I occasionally announce on my blog (when people email me about it :-)
In depth: Likely not. I've actually been working on another literate language tutorial (completely unrelated to FORTH), so ... watch this space (or my blog).