Live data from Hacker News

What the hell is Forth? (2019)

blog.information-superhighway.net

101–110 of 138 posts

Re: What the hell is Forth? (2019)

#101

If you want to build a Forth check out R. G. Loeliger's "Threaded Interpretive Languages Their Design And Implementation" https://archive.org/details/R.G.LoeligerThreadedInterpretive... (Jonesforth has already been mentioned.) Read "Starting Forth" and "Thinking Forth" too, even if you don't want to write a Forth or program in Forth. It'll still expand your mind in useful and fun ways.

You can also read the following "tutorial" on implementing the basic facilities of a forth right here:

https://news.ycombinator.com/item?id=13082825

I used that as a starting point to implement a trivial forth in golang.

Re: What the hell is Forth? (2019)

#102
post #5

Earlier quoted context omitted.

Agree with the parent, it's a language worth knowing like LISP is worth knowing, but (unlike LISP) it is not a good language choice for almost all use cases in 2023 (almost all use cases are not that memory constrained any more, even embedded systems), for the same reason you shouldn't use Perl (it is very hard to read code). I was going to say "its main disadvantage is that the only way to find out what a piece of c…

> for the same reason you shouldn't use Perl (it is very hard to read code) I slightly disagree with this take on Perl. Agreed that it is easy to write code which is hard for others to read, and agreed that it is often not a good idea in a large-team setting, for that reason. But there is a lot of code that doesn't fall in that category, and Perl, by its nature (and by CPAN), can be remarkably good at just "getting s…

Not trying to disparage Perl, which has some utility, like most languages, notably fast regex engine and presence on many systems.

BUT: I've been writing Perl for much longer than Python, but despite wanting to write readable/maintainable code, Perl has a tendency to get you to a stage where your code looks cryptic (part of it is by design - Larry Wall, a linguist, values implicitness as "context" - I think explicitness, by contrast, should be aspired to, in order to avoid confusion). I've never written cryptic Python code (and I am not a particular Python fan, as I prefer statically typed compiled languages), but it should be said that clarity is one of Python's advantages.

I agree with you that it is easier to write readable Perl than readable FORTH, which could be due to the similarity of Perl with other non-stack imperative languages.

Re: What the hell is Forth? (2019)

#103
Lots of virtual machines are stack machines and lots of languages compile to bytecode for a virtual machine. I thought that bytecode being a language in its own right would be a good idea so tried using Forth for it. Had a rough plan of a toolchain resembling a ML implemented in a lisp compiled to a forth. There's an article (by Baker?) which showed recursion and iteration folding to the same code on a stack machine which is beautiful.

That project was an abject failure. I was not good at programming stack machines. I can't keep track of where things are on the stack. The Forth way seems to be using lots of short functions (uh, words) with predictable stack behaviour that compose to more complicated behaviour, and I struggle to keep track of many small functions too. Both combined is really tough. I essentially need to use a compiler to reliably target the stack VM.

It turns out I type while I think and that yields large, sprawling functions with dead paths and mistakes. That's then refined and factored as my thinking clears. A repl sort of works for this but there's a lot of reaching back to gather the pieces where the working ones are not clearly distinguished from the failed experiments. The large messy work in progress function strategy did not work at all in forth.

It's surely possible to retrain oneself to think the Forth way. Might be worthwhile, though it's hard to guess the value trade-off before doing so. People tell me it's worthwhile, and they were right about that with lisp, so I'm probably missing out.

Re: What the hell is Forth? (2019)

#104
Last year's Advent of Code I dived into Forth a bit (used GForth). Solved 5 puzzles in it, but then I got stuck, when I was reading in the input of a puzzle and the lines in the file were of varying length. I did not find a way to properly clear the previous line buffer and got wrong results because it still contained characters from an earlier longer line. I asked around for a solution but did not get anything helpful that always works. I think more time would be needed to truly get good at it. Learning some patterns that are typically used in the language to do things like reading a file line by line and processing it. Some string related stuff is supposed to become better in GForth soon, when 1.0 is released. Good to know!

That said, I did enjoy it sort of, until I got stuck. Sometimes definitions can be very succinct and it made me think about implicit arguments vs explicit arguments to functions. In Forth all is implicitly on the stack. It has to be there already, when you utter a word, otherwise you are making a mistake. That is of course harder to program correctly, but makes code very short. Also gets you naturally into the territory of higher order functions and point-free style.

What I think could be an issue with existing programs is, that one needs to build and memorize a mental model of all the implicit arguments words take. It adds one more lookup to the definition of a word. But perhaps this is a tradeoff for very readable code (if written very well, very well factored).

I also thought about "How could this ever do multicore?" and "How can any data structure be better than linear time, if everything is on a stack?". GForth seems to have stuff for making threads, but I have not gone that far on my journey.

Just try it. It is an interesting experience.

Re: What the hell is Forth? (2019)

#105

Lots of virtual machines are stack machines and lots of languages compile to bytecode for a virtual machine. I thought that bytecode being a language in its own right would be a good idea so tried using Forth for it. Had a rough plan of a toolchain resembling a ML implemented in a lisp compiled to a forth. There's an article (by Baker?) which showed recursion and iteration folding to the same code on a stack machine…

Forth people like to factor code into very short words, to help avoid confusion with the stack. Words should be properly commented so that their stack effects are clear.

https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Fac...

Re: What the hell is Forth? (2019)

#106
post #100

Earlier quoted context omitted.

It is a grave error to introduce Forth without noting that it is universally recognized as the First Programming Religion. Lately Lisp and Rust have begun to take on religious overtones, but Forth came first. It is probably also a mistake to introduce it without mentioning that the first two (normally one-character) words defined are pronounced "builds" and "does".

Lately? Lisp has been a religion for decades.

Decades counts as lately. Are there any other programming religions?

Re: What the hell is Forth? (2019)

#107

I started using Forth because I wanted a Reverse Polish Notation calculator at the command line. It turned out to be very practical and extensible. For example, I created many words to perform unit conversion, such as temperature: : 2fahrenheit 9 * 5 / 32 + ; : 2celsius 32 - 5 * 9 / ; Furthermore, I can even pull in live currency conversion rates from Yahoo Finance to go between USD and CAD. Since my underlying Forth…

Something I like about Forth is, that one can use almost anything in identifier names ant any position, because the language does not have arbitrary limitations in that like mainstream langs have. I also like that in Scheme. Very useful for making shorter and meaningful names.

Re: What the hell is Forth? (2019)

#108
post #100

Earlier quoted context omitted.

Lately? Lisp has been a religion for decades.

Decades counts as lately. Are there any other programming religions?

Haskell, Julia, Rust, Lisp, and Forth are 'esoteric' enough to create an in-crowd, who are quite often deluded by imagined miraculous solutions to people's problems.

Please repeat after Fred Brooks: there ain't no silver bullet.

To a certain extent this goes on outside programming languages per se: OOP, Gang of Four, Agile match the pattern as well.

Re: What the hell is Forth? (2019)

#109
post #5

Earlier quoted context omitted.

Agree with the parent, it's a language worth knowing like LISP is worth knowing, but (unlike LISP) it is not a good language choice for almost all use cases in 2023 (almost all use cases are not that memory constrained any more, even embedded systems), for the same reason you shouldn't use Perl (it is very hard to read code). I was going to say "its main disadvantage is that the only way to find out what a piece of c…

> for the same reason you shouldn't use Perl (it is very hard to read code) I slightly disagree with this take on Perl. Agreed that it is easy to write code which is hard for others to read, and agreed that it is often not a good idea in a large-team setting, for that reason. But there is a lot of code that doesn't fall in that category, and Perl, by its nature (and by CPAN), can be remarkably good at just "getting s…

Well, in Forth there isn't much "wild syntax" to begin with, so naturally you couldn't claim the same about Forth.

Re: What the hell is Forth? (2019)

#110

Earlier quoted context omitted.

Decades counts as lately. Are there any other programming religions?

Haskell, Julia, Rust, Lisp, and Forth are 'esoteric' enough to create an in-crowd, who are quite often deluded by imagined miraculous solutions to people's problems. Please repeat after Fred Brooks: there ain't no silver bullet. To a certain extent this goes on outside programming languages per se: OOP, Gang of Four, Agile match the pattern as well.

[dead]
Post reply on HN