4 ms·
It is relatively easy. You can look at stack as a list: type Stack = [Int] Adding two values at top of stack is easy: add (a:b:stk) = (a+b) : stk Dropping
by thesz 7y ago
It is relatively easy.
You can look at stack as a list:
type Stack = [Int]
Adding two values at top of stack is easy:
add (a:b:stk) = (a+b) : stk
Dropping and duping is also easy:
drop (_:stk) = stk
dup (x:stk) = x:x:stk
double stk = add (dup stk)
You can encode list using Church encoding [1] and voila! You just connected lambda calculus and stack machine (not Forth, but close).
[1] https://en.wikipedia.org/wiki/Church_encoding#List_encodings https://en.wikipedia.org/wiki/Church_encoding#List_encodings
Forth include more that just stack machine - return stack, vocabulary, definintions, memory, etc. This means that these simple definitions will not be as simple as above for complete Forth. But they also can be represented as Church encoded data and they still be representable in lambda calculus.