Re:Starting Forth – the classic Forth language tutorial
by bjz_ on Thursday June 20, 2013 @06:10AM
(#5911179)
Factor is another interesting concatenative programming language (inspired by Forth): http://concatenative.org/ I've been playing around with it for the past few days and found it to be very well documented. Well worth a look if you're looking for something to bend you mind in new ways!
Re:Starting Forth – the classic Forth language tutorial
by ggchappell on Friday June 21, 2013 @06:57AM
(#5917401)
I like Factor a lot. Forth has always struck me as something with potential; but, alas, it's stuck in the 70s. Data structures are painful. You can forget about anything like generics, polymorphism, interfaces, etc. It supports functional programming poorly. It fails to benefit from modern ideas about modules/namespaces or type systems that stay out of your way. And there is no protection from anything at all. When I look at Factor, I find myself thinking, "This is what Forth should have been."
So, yes, Factor is worth a look. Certainly as a mind-expander, and possibly as a practical production language (just guessing here).
Re:Starting Forth – the classic Forth language tutorial
by fizfaz on Thursday June 20, 2013 @04:49AM
(#5910950)
Just wanted to mention that Thinking Forth "A Language
and Philosophy for Solving Problems" (also from Leo Brodie) is available here: http://thinking-forth.sourceforge.net
Re:Starting Forth – the classic Forth language tutorial
by liyanchang on Thursday June 20, 2013 @03:16AM
(#5910675)
tl;dr
Forth is a programming language from the 1970s. This is the canonical book, published by someone who works at Forth Inc. (as K&R is to C)
Forth is interesting, not because of widespread usage, but because it has a different way of looking at things. Reminiscent of people who tell you that you should learn LISP because you'll never use it, but it will make you a better programmer.
The key difference is that it is stack based. (For those who are more mathy, you may have heard of RPN on the HP calculators)
e.g.
2 5 * 3 -
is take 2 and 5, multiply them. Take the result and 3, subtract them. get 7.
Re:Starting Forth – the classic Forth language tutorial
by tikhonj on Thursday June 20, 2013 @07:14AM
(#5911363)
I've always wanted to write a Forth tutorial just to take advantage of all the "forth" puns that come to mind. I could call it "setting Forth" :).
Seriously though, I could probably write something interesting about ArrayForth and GreenArray chips--I've been playing around with them for a while and even wrote an emulator for their instruction set. Yet another topic in the queue for my blog, which doesn't even exist yet :P.
Re:Starting Forth – the classic Forth language tutorial
by robomartin on Thursday June 20, 2013 @12:23AM
(#5910128)
I love Forth and used it extensively for many years. I wonder how much real use it is seeing today. I remember a situation many years ago where a friend of mine chose to code his embedded product in Forth. As the company grew it became difficult to find programmers. As things got to the point where acquisition started to become a possibility Forth became a liability. He had to convert the entire code base to C in order to remove objections. I pitched in and helped with the effort. While it wasn't terribly difficult it did take time and cost money.
That experience probably marked the last time I used Forth on an embedded system. Every time I considered the idea for my own work I remembered what happened to him. As a result I pretty much stayed with C/C++ for embedded work from that point forward.
My gut feeling is that today Forth is a liability for most applications outside cases where there might be an existing code base that must be leveraged.
EDIT: I still think it is important for programmers to learn about TIL's and Forth. You'd gain valuable perspective and a different way to see and approach problems.
Re:Starting Forth – the classic Forth language tutorial
by bcoates on Thursday June 20, 2013 @02:50AM
(#5910614)
Stories like this always astound me. Learning Forth is probably less difficult than the average GUI widget library. Anyone who I'd trust around a C compiler could probably become a serviceable Forth coder in a weekend.
Do moderately bright kids not get exposed to HP RPN calculators anymore?
Re:Starting Forth – the classic Forth language tutorial
by robomartin on Friday June 21, 2013 @08:05PM
(#5921881)
Well, for what it's worth all we have at home are HP calculators. I taught my oldest kid to use them. He's the only kid in his school with an HP41. His math and science teachers didn't know how to use it.
Re:Starting Forth – the classic Forth language tutorial
by dvdkhlng on Thursday June 20, 2013 @04:10AM
(#5910845)
The WikiReader does have a Forth interpreter embedded in its firmware, but doesn't run it by default. It is activated when pressing the right buttons at boot and allows developers to access a Forth console via serial debug-cable and/or compile Forth programs from Forth sources contained in the built-in micro-SD card.
As Forth is very low-level and allows direct access to memory (and thus memory-mapped registers), it makes testing hardware quite efficient (and fun).
What makes you believe the WikiReader didn't sell well? AFAIR quite the opposite.
Re:Starting Forth – the classic Forth language tutorial
by kabdib on Thursday June 20, 2013 @08:03AM
(#5911522)
A friend of mine wrote a big embedded system in FORTH. He was really pragmatic, and his code was good. It's one of the few success stories of FORTH that I know of: Very well structured, well-documented, and it performed well.
A few years later he and his team had converted it to C, too, since they couldn't find people to work on it.
I recently re-read _Starting FORTH_ and was again struck by how simple the language is. It's perfect for things like system bringup (which is what we used it for at Atari) and other small applications where you don't need complex data structures or hard real time operation, and your team is small.
Beyond that, FORTH doesn't appear to scale. That's also an important lesson.
Every embedded engineer should write a FORTH kernel. It's small enough to take just a few days to get something running. It's a valuable lesson in "just enough, and lots less than you think you need".
Re:Starting Forth – the classic Forth language tutorial
by rwmj on Thursday June 20, 2013 @08:12AM
(#5911542)
That was certainly my experience writing a small FORTH kernel. The immediate problems are: lack of dynamic allocation, and poor handling of complex functions.
Of course you can write your own dynamic allocation routines, but that will make your FORTH less than simple. [I'll assume here that garbage collection is probably out of scope for tiny embedded systems -- I'm just talking about lack of malloc/free-like functionality]
The standard advice is to break up functions into smaller and smaller units, but I found you can only go so far doing that, and in any case it just leads to having lots of small functions to understand which is a type of complexity in itself. Being effectively forced to use the stack to store and pass intermediate results around is highly non-intuitive for programmers used to more common languages.
Re:Starting Forth – the classic Forth language tutorial
by astrobe_ on Friday June 21, 2013 @09:21AM
(#5917878)
> the immediate problems are: lack of dynamic allocation
That's not an "immediate" problem, far from it. I've written a standalone Forth system. I could boot it from a floppy, modify its source, and recompile it in less than a minute on an anachronistic 8086-based laptop. Then I ported to C in order to be able to play with the zillions of libraries one can find on Windows and Linux, to play with GUI and 3D Engines. I did implement heap functions, but it was more for the fun of it than because I needed them.
> poor handling of complex functions.
Other languages would let you create function/methods that spread over dozens of pages, but this is universally considered bad form. Forth isn't designed for bad form, which is indeed a serious competitive handicap nowasdays.
> Of course you can write your own dynamic allocation routines, but that will make your FORTH less than simple
So you either say "Forth sucks" or you can say "Are there better ways?". If you add up the overhead of heap allocation structures, the size of the malloc/free functions and the overhead of calling malloc/free, you'll find out that often, over-allocating static structures is just as good, memory-wise. Moreover most systems, including many embedded systems, have plenty of resources. So it doesn't really matter if you waste some kilobytes because you're over-allocating.
> having lots of small functions to understand which is a type of complexity in itself
Ravioli code versus Spaghetti code has always been a conundrum. In the end you just can't set an arbitrary limits on the size of definitions/functions/methods. I think the correct criteria is that you definitions are semantic units. As Brodie points out in Thinking Forth, if you can describe your definition in one short sentence, it is ok. If you do that and still end up with lots of definitions, it is likely that the problem you are trying to solve is itself complex.
> highly non-intuitive for programmers used to more common languages
That's to me a funny reproach, because my definition of a programmer is to be able to absorb and adapt to new languages, styles and paradigms in no time. You know that feeling when you try to explain what's your job exactly and people think you're talking witchcraft? Our job is to deal with non-intuitive, occult siliconic forces.
Re:Starting Forth – the classic Forth language tutorial
by astrobe_ on Friday June 21, 2013 @09:43AM
(#5917961)
I don't see where you fetch your claims from. "hard real time operation", for instance, is the responsability of the operating system. If Forth is also the OS, nothing prevents anyone from implementing real time features like in any other operating systems. "Small teams" is common practice since Brooks, even in "big projects", where the workload is split among small internal teams. "Scale": that word means nothing without a dimension. Learning Forth takes a few days, but writing one rather takes a few weeks the first time, in particular if you want to go beyond the trivial "Forth in language X" stuff that doesn't learn you a lot about how KISS operates in Forth.
Re:Starting Forth – the classic Forth language tutorial
by zaf on Thursday June 20, 2013 @03:31AM
(#5910713)
I've just recently started to dig into stack-based programming languages and would love to get hold of the Leo Brodie manuals.
One question to programming language experts: Is it possible to have a stack-based programming like forth but without the need for the extra characters needed in the syntax (like quotes, brackets etc..) and only use whitespace to separate the 'words'?
Re:Starting Forth – the classic Forth language tutorial
by dvdkhlng on Thursday June 20, 2013 @03:45AM
(#5910765)
Stack-based programming does not need extra syntax characters (what you call quotes and brackets) to structure the program.
arithmetic syntax: (a+b)*c
lisp syntax (polish notation): (* (+ a b) c)
forth syntax (reverse polish notation): a b + c *
In fact all words in Forth are only separated by whitespace. However, to "emulate" more complex syntax, some words in Forth are marked to be executed immediately upon parsing (somewhat like "macros" in lisp) and then access the input stream to extract more than whitespace-separated tokens. Exampes:
Re:Starting Forth – the classic Forth language tutorial
by dvdkhlng on Thursday June 20, 2013 @04:30AM
(#5910898)
There are various levels of abstraction when accessing the input stream. The lowest level is by inspecting the input (line) buffer. Just realized how that allows one to write a three-word quine [1] in ANS-compliant Forth:
Re:Starting Forth – the classic Forth language tutorial
by bjpcjp on Thursday June 20, 2013 @06:55AM
(#5911292)
My first job after grad school with at Harris Semiconductor. We were selling RTX processors which natively ran Forth instructions. (The architecture was completely stack-based.)
I agree that Forth is a liability today. I could see it being used as an introductory language - in junior high school, for example - but not much beyond that.
Re:Starting Forth – the classic Forth language tutorial
by jacques_chester on Thursday June 20, 2013 @01:18AM
(#5910323)
I have what I believe is a first edition. I grabbed it from the giveaway pile at my university, thinking it was not the kind of book you could buy anymore.
So, yes, Factor is worth a look. Certainly as a mind-expander, and possibly as a practical production language (just guessing here).
http://forthfreak.net/jsforth80x25.html
I learned a lot from this book.
Forth is a programming language from the 1970s. This is the canonical book, published by someone who works at Forth Inc. (as K&R is to C)
Forth is interesting, not because of widespread usage, but because it has a different way of looking at things. Reminiscent of people who tell you that you should learn LISP because you'll never use it, but it will make you a better programmer.
The key difference is that it is stack based. (For those who are more mathy, you may have heard of RPN on the HP calculators)
e.g. 2 5 * 3 -
is take 2 and 5, multiply them. Take the result and 3, subtract them. get 7.
The wiki article has a nice picture of stacks that may help: http://en.wikipedia.org/wiki/Forth_(programming_language)
(btw, not super knowledgable about this stuff; just super confused, did some digging and figured others would be confused as well. Happy to edit.)
EDIT 1: Habits die hard. Corrected Fourth to Forth.
Start with 'jonesforth.S'
Seriously though, I could probably write something interesting about ArrayForth and GreenArray chips--I've been playing around with them for a while and even wrote an emulator for their instruction set. Yet another topic in the queue for my blog, which doesn't even exist yet :P.
That experience probably marked the last time I used Forth on an embedded system. Every time I considered the idea for my own work I remembered what happened to him. As a result I pretty much stayed with C/C++ for embedded work from that point forward.
My gut feeling is that today Forth is a liability for most applications outside cases where there might be an existing code base that must be leveraged.
EDIT: I still think it is important for programmers to learn about TIL's and Forth. You'd gain valuable perspective and a different way to see and approach problems.
Do moderately bright kids not get exposed to HP RPN calculators anymore?
No, they learn BASIC (maybe) on the TI graphing calculators.
[1] http://yannramin.com/2009/10/16/the-wikireader-cool-device-f...
As Forth is very low-level and allows direct access to memory (and thus memory-mapped registers), it makes testing hardware quite efficient (and fun).
What makes you believe the WikiReader didn't sell well? AFAIR quite the opposite.
I'm actually considering getting one of these for that price, would be an interesting device to tinker with FORTH.
A few years later he and his team had converted it to C, too, since they couldn't find people to work on it.
I recently re-read _Starting FORTH_ and was again struck by how simple the language is. It's perfect for things like system bringup (which is what we used it for at Atari) and other small applications where you don't need complex data structures or hard real time operation, and your team is small.
Beyond that, FORTH doesn't appear to scale. That's also an important lesson.
Every embedded engineer should write a FORTH kernel. It's small enough to take just a few days to get something running. It's a valuable lesson in "just enough, and lots less than you think you need".
Of course you can write your own dynamic allocation routines, but that will make your FORTH less than simple. [I'll assume here that garbage collection is probably out of scope for tiny embedded systems -- I'm just talking about lack of malloc/free-like functionality]
The standard advice is to break up functions into smaller and smaller units, but I found you can only go so far doing that, and in any case it just leads to having lots of small functions to understand which is a type of complexity in itself. Being effectively forced to use the stack to store and pass intermediate results around is highly non-intuitive for programmers used to more common languages.
That's not an "immediate" problem, far from it. I've written a standalone Forth system. I could boot it from a floppy, modify its source, and recompile it in less than a minute on an anachronistic 8086-based laptop. Then I ported to C in order to be able to play with the zillions of libraries one can find on Windows and Linux, to play with GUI and 3D Engines. I did implement heap functions, but it was more for the fun of it than because I needed them.
> poor handling of complex functions.
Other languages would let you create function/methods that spread over dozens of pages, but this is universally considered bad form. Forth isn't designed for bad form, which is indeed a serious competitive handicap nowasdays.
> Of course you can write your own dynamic allocation routines, but that will make your FORTH less than simple
So you either say "Forth sucks" or you can say "Are there better ways?". If you add up the overhead of heap allocation structures, the size of the malloc/free functions and the overhead of calling malloc/free, you'll find out that often, over-allocating static structures is just as good, memory-wise. Moreover most systems, including many embedded systems, have plenty of resources. So it doesn't really matter if you waste some kilobytes because you're over-allocating.
> having lots of small functions to understand which is a type of complexity in itself
Ravioli code versus Spaghetti code has always been a conundrum. In the end you just can't set an arbitrary limits on the size of definitions/functions/methods. I think the correct criteria is that you definitions are semantic units. As Brodie points out in Thinking Forth, if you can describe your definition in one short sentence, it is ok. If you do that and still end up with lots of definitions, it is likely that the problem you are trying to solve is itself complex.
> highly non-intuitive for programmers used to more common languages
That's to me a funny reproach, because my definition of a programmer is to be able to absorb and adapt to new languages, styles and paradigms in no time. You know that feeling when you try to explain what's your job exactly and people think you're talking witchcraft? Our job is to deal with non-intuitive, occult siliconic forces.
One question to programming language experts: Is it possible to have a stack-based programming like forth but without the need for the extra characters needed in the syntax (like quotes, brackets etc..) and only use whitespace to separate the 'words'?
Thanks for the explanation. I really ought to do a practical session with Forth.
Have a look at Rebol which is a mix of Forth, Logo & Lisp.
Here's an example of Rebol code (typed into the console (REPL))...
multiply and add are both functions which take two arguments. If I add parens then above becomes clearer. So Rebol evaluates a list of words from left to right with non-variadic functions (ie. they have fixed argument arity).Here's an example of a conditional:
if is a function which takes two arguments (as is equal?). So this is code is evaluated like this: If the first argument is true (which it is in this case!) then the second argument (which is a block) is evaluated...I agree that Forth is a liability today. I could see it being used as an introductory language - in junior high school, for example - but not much beyond that.
http://www.jeforth.com
Great to see it online.