3 ms·
> Was I missing something? Is there a way to let Forth keep track of where on the stack "foo" lives if it is needed several times in a subroutine instead of alw
by JadeNB 4y ago
> Was I missing something? Is there a way to let Forth keep track of where on the stack "foo" lives if it is needed several times in a subroutine instead of always shuffling the stack or counting the elements on it?
I'm not a Forth novice, much less an expert, but my impression is that the answer is that if you spend much time thinking about the location of variables, then you're "doing it wrong"—to steal the phrase, trying to write C in Forth, rather than to write Forth. My understanding is that the emphasis is on building highly domain-specific languages out of many small words, each of which is re-factored so much that it never needs to keep much track of any, or much, shuffling of variables on the stack.
- dottedmag 4y agoSo basically "use it as a language for combinators", right? But combinator languages are pretty hard to read no matter what one does, don't they?
- kragen 4y agono forth is ruthlessly imperative c: x = a[n-1]; n += y * x; plot(x, y, n); forth: : foo n @ 1- a @ x ! ; \ ‡ : bar y @ x @ * n +! ; : baz x @ y @ n @ plot ; : quux foo bar baz ; naming each line of code allows you to do things like bar n ? to see what n is after you bar in exchange for the somewhat rebarbative syntax and lack of typing you get turing-complete macros (with full compiler access, without the pitfalls of string-replacement macros) and an interactive repl that fits into almost the smallest microcontrollers because you have total freedom to redefine the syntax you can construct whatever dsl you prefer it is nothing like combinatory logic except that you can occasionally write a point-free function ______ ‡ this assumes a is defined as something like this : array create cells allot does> swap cells + ; 20 array a so that 3 a computes an &a[3] suitable for @ and !, etc.