4 ms·
Was I missing something? yes use variable or value for variables, not the stack the stack is for expression evaluation roughly everyone has this problem at
by kragen 4y ago
Was I missing something?
yes
use variable or value for variables, not the stack
the stack is for expression evaluation
roughly everyone has this problem at first
if you'd put it in a local variable in c or js, 85% of the time you should put it in a variable in forth. start with 100% or 110% and scale back from there
yes this means your words aren't reentrant by default
this is ok, most subroutines aren't recursive
never shuffle the stack or count the elements in it. if you find yourself doing that, put the elements you wanted to access in a variable or value
then you can access them by name. this is much easier
the possibility of shuffling the stack is an attractive nuisance, like a tar pit. an occasional dup or swap or r@ is okay, but start by not using them
literally write square as
variable x
: square x ! x @ x @ * ;
or
value x
: square to x x x * ;
if that's what it takes to stop tying your code in knots
it is ok to name a variable x, its scope only extends to the next definition of an x. it's not like in c where every x needs to either be local to a subroutine or a single global unique x used by the entire program (or at least source file)
later you can graduate to sometimes replacing one local variable with the return stack and another one or occasionally two with the operand stack, but it is better for your code to be kind of boring and verbose than an incomprehensible soup of 2dup -rot tuck
forth is maybe not better than c but there is no reason to make it worse
- dottedmag 4y agoThis 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 ago
- carapace 4y agoDid you ever read "Thinking Forth"?
- kragen 4y agocoincidentally i gave away my paper copy last night, six days after your comment and, as it turns out, 25 years after buying it
- kragen 4y agoa few times, mostly last millennium
- carapace 4y agoIn that case, your Forth style is fascinating. Cheers! :)
- kragen 4y agodon't get me wrong, i do think : square dup * ; is better code but the possibility of such clean, concise definitions is a seductive trap; it is easy to write code that isn't obviously correct in pursuit of that aim quickly this leads you to code that is not just unreadable but full of bugs, and that is how the person who started this thread was, in their words, trying forth and failing; this is not just a common experience but nowadays a nearly universal one so it is much better to err on the side of too many variables and too little stack manipulation than the opposite you can always streamline the code later once it works i'm pretty sure leo brodie would agree with me on this