3 ms·
This is roughly equivalent to C using "static globals" instead of named local variables, right?
by dottedmag 4y ago
This is roughly equivalent to C using "static globals" instead of named local variables, right?
- kragen 4y agothere are some important differences i just edited in a note about one of them, the scoping for large programs forth has namespaces (vocabularies or wordlists) similar to static file scope in c an important similarity between standard forth variables and c static variables is that they don't support recursion. in the occasional case where you do need recursion you can use the operand stack to save and restore your variables manually x @ y @ ... recurse ... y ! x ! which would not be possible in c with universal use of static global variables also though typically when you are stepping through forth you are doing it by typing names of words, while in c you use the debugger, which can see and change local variables
- p_l 4y agoSeems to me more like declaring variables in C as static, inside function definitions. The names might be duplicated, but inside that function it will always point to the same common memory location.
- kragen 4y agoyes, it's similar in that every function can have its own variable x if you want, but an important difference is that you can share the same variable between several forth words, as in my example in https://news.ycombinator.com/item?id=33786716 https://news.ycombinator.com/item?id=33786716, even if you also have other variables of the same name used by other groups of words this allows you to make your words more like 'named lines of code' or 'named parameters' than like c functions, which is helpful for interactively poking at the system, and that's where forth really shines like historically, as i understand it, the ui to the forth, inc. fullscreen text editor was a forth repl, just using a vocabulary with a lot of one-letter words for editor commands. that's not practical even with lisp; imagine trying to use emacs with only m-: as your keyboard interface. but with forth the interaction friction is only roughly as bad as with ed or ex, with the bonus that you can see what you're doing in theory making your words smaller helps with composability as well, but having hidden state shared between the words in variables does tend to undermine that benefit imho
- abecedarius 4y agoAt FORTH, Inc. in the 80s they programmed the keyboard to make the arrow keys (and such) macro-expand into editor-vocabulary commands. So the Forth-repl editor had interactivity sort of halfway between ed and Emacs. (Related anecdote: my summer job there back then was to review the new version of their system to find bugs, etc. There was this one piece which was ugly and awkwardly coded, and I was kinda happy to find it: finally, an excuse to rewrite something! It was the conventional interactive editor. Turned out it had been contributed by a customer; absolutely nobody at the company used it. Forth editor all the way.)
- kragen 4y agothank you for the correction; i was relaying my poorly-remembered account of what you said about the experience, as you likely guessed