4 ms·
I've seen a lot of embedded Forth stuff recently but I don't get the appeal. Clearly there is some benefit over writing assembly, but why is it preferred over p
by sandwell 6y ago
I've seen a lot of embedded Forth stuff recently but I don't get the appeal. Clearly there is some benefit over writing assembly, but why is it preferred over portable C, or even higher level languages such as Python on more powerful mcus?
FWIW I felt the same way about LISP for a long time until I fell in love with it via Emacs. So please educate me!
- sn41 6y agoForth is not just another language for the sake of it. I am currently learning Forth from Leo Brodie's excellent books "Starting Forth" and "Thinking Forth". It actually changes the way you think. Just as Lisp does - almost a feeling of "a new planet swims into his ken". The main feature of the Forth style is to write lots of 1 to 2 line functions, in such a way that at the high-level, you have essentially a domain-specific language that looks like exactly what you want a program to look like - for example, "RINSE WASH CYCLE SPIN STOP" for a washing machine program, and "RINSE WASH RINSE WASH CYCLE SPIN STOP" for an extra clean option. People claim that once you learn Forth, you start programming Forth-style in other languages like C, Python etc. I can already see the appeal.
- DiabloD3 6y agoLisp and Erlang also do this. My C sometimes looks like Lispy Erlangish weirdness, and my Java often looks like my C. Certain languages just have certain ways of worming their way into your brain and improving the way you think, much to the dismay of everyone who can't think that way yet because the idiom isn't native to that language (yet).
- sandwell 6y agoI love that effect. My JS changed a lot once I grokked LISP, and I finally understood why there was a Haskell course on my degree track. One of the most rewarding training experiences was giving my old Haskell textbook to a junior team member and watching his (at the time .NET, w/LINQ) code change over the course of a few weekends.
- reddit_clone 6y agoYep. My regular programming has changed after learning lisp/erlang/haskell. For example I am writing functions with no (or less) mutation and keeping them pure. Makes for easy testing. We have Ruby programmers who has never used map/fold etc.
- sandwell 6y ago> People claim that once you learn Forth, you start programming Forth-style in other languages like C, Python etc. Sounds very much like my experience with LISPs. Probably just need to dive in and try it. Thanks for the detailed response.
- flohofwoe 6y agoI don't have recent experience with Forth, but traditionally, Forth didn't require such a "beefy" machine as a C programming environment. For instance here's a fairly complete Forth environment in around 10 KBytes running on an 8-bit home computer. There was no C compiler at all for that machine, and if there would be, working with it would be a lot more hassle than a Forth REPL. https://floooh.github.io/tiny8bit/z1013.html?type=z1013_64&file=z1013/z1013_forth.z80 https://floooh.github.io/tiny8bit/z1013.html?type=z1013_64&f... What's the appeal today, I don't know. But I'm in the "curly braces" camp. If I'd be a Lisp guy, Forth would probably be more appealing to me.
- sandwell 6y agoMakes sense. I have a ZX Spectrum on my workbench and a copy of Sinclair Forth somewhere, maybe it will click for me in a more restricted environment.
- carapace 6y agoLegend has it that Chuck Moore used to carry around a 3D wireframe CAD program... ...on a deck of punchcards in his shirt pocket.
- exikyut 6y agoAw, input is broken on mobile (Android + of course Chrome). I'm getting a keyboard popup, and I appear to be typing somewhere offscreen (I can see a typing suggestion containing exactly what I've typed, which wouldn't happen if input wasn't going anywhere), but the text isn't appearing onscreen, then when I press Enter, I get an "OK" as though I'd hit Enter on a blank line... and the input I'd typed isn't cleared. So, "words <Enter> words <Enter>" shows "wordswords" in my keyboard's suggestion, and "OK\nOK\n" on the display. Looks really cool though :D
- flohofwoe 6y agoYeah no useful mobile support unfortunately. Plan is to replace the standard virtual keyboards (which don't have the necessary keys anyway) with a custom-rendered keyboard eventually.
- LeifCarrotson 6y agoForth has a REPL, and tiny Forths like this can work on microcontrollers that are unable to host a Python instance. (A microcontroller that can run Python is pretty rare). Otherwise, your development cycle in C or assembly is "peer at datasheet, carefully enter bitwise arithmetic, compile, flash, wait several seconds for flashing to complete, debug."
- hansvm 6y agoThrough a certain lens, a vanilla Forth program is defined purely by composing pure functions (and those compositions are vastly easier to write in Forth vs C or Python). It's a simple idea that has a lot of elegance, giving you high-level properties in a language that can be very close to the metal.
- upofadown 6y ago>... but why is it preferred over portable C,... C normally involves compiling the entire program at once. Even if you use a interpreted C of some sort, C is a lot more verbose and involves more typing. This is important when testing and evaluating new hardware. All you need is a serial port or equivalent connection to a forth to allow you to exercise the hardware in a very interactive way. In some cases you might end up writing the final program by accident.
- nine_k 6y agoAdding to what is already said: * Forth is tiny. You don't need a preprocessor, a compiler, a linker. Your entire programming environment, beside the text editor, can live directly on the target MCU. * Forth is interactive. You can run your code, examine the results, define and redefine words, run again, etc right on the target machine (which may be a tiny controller). Yes, you likely will remove the REPL-related words from the final image (or not), but C can't hold a candle to the interactive development experience of Forth. * Unlike in C, you can build an economical DSL in Forth to closely match your target domain. What Forth takes away in readability due to the need to think in stack terms, it more than pays back in expressiveness and abstraction, provided certain skill and taste. While Forth does have an execution overhead and thus cannot obviate C completely in some cases, I've seen e.g. an injection controller for an internal combustion engine which did hard realtime completely in Forth. Hard realtime here means that all computations were clocked by the rotation of the shaft, with precise timing of different phases along the way.