4 ms·
Lisp is Polish Notation but Forth is Reverse PN. I wonder if the same constructs can be made in terms of the other language.
by huachimingo 5y ago
Lisp is Polish Notation but Forth is Reverse PN.
I wonder if the same constructs can be made in terms of the other language.
- js8 5y agoNo. There are three fundamental ways to encode a syntax tree - prefix, infix, and postfix. Haskell (ML) is prefix, Lisp is infix, and Forth is postfix. (I.e. Forth-like is the opposite of ML-like, not Lisp-like.) The advantage of infix is that you don't need to know the argument counts for functions/macros to reconstruct the tree, but the disadvantage is extra parens. I am slowly coming to (subjective) conclusion that postfix is the most natural, because it's the same as the (typical) evaluation order.
- moonchild 5y agoWhy should I concern myself with the order of evaluation? I want to think about my programs from the top down, not the bottom up. What about laziness, which disrupts this apparent order and in fact evaluates from the outside in?
- js8 5y agoYou don't have to anything, I said it's subjective. I prefer ML-like to Lisp-like, because I want typechecking anyway (which involves counting arguments) and this means most Lisp parens we can dispose of. And lately, I noticed I use a lot of "$" (apply) in my code, and while it reads left-to-right, it mostly evaluates right-to-left. So why not switch to Forth-like, which reads and (mostly) evaluates left-to-right?
- dreamcompiler 5y ago> Haskell (ML) is prefix, Lisp is infix, and Forth is postfix. With respect, no. Lisp is by default prefix (Polish notation) but with some work you can make it behave as infix or postfix. Haskell is naturally either prefix or infix depending on how you define and call your functions (the use of backticks or parens around the function name in a call changes it from prefix to infix and vice-versa. It's a rather elegant solution to the problem.) As you stated Forth is postfix (Reverse Polish notation).
- dreamcompiler 5y agoI've built the beginnings of a Forth compiler in Common Lisp just to prove the concept. I find Forth syntax more convenient for some tasks (like emitting a series of HTML constructs to a stream, in precise order) than Lisp syntax and since Common Lisp gives you access to the values stack it's pretty easy to turn it into Forth. Of course you have to give up variadic functions, but that's the nature of Forth. I haven't tried the reverse (building a Lisp in Forth) but I imagine it would be straightforward. One can build a Lisp from just about anything.