New ask Hacker News story: Ask HN: Why do people say "Lisp has no syntax"? It has infinite syntax
Ask HN: Why do people say "Lisp has no syntax"? It has infinite syntax
4 by behnamoh | 3 comments on Hacker News.
A popular notion is that Lisp has no syntax. People also say Lisp's syntax is just the one rule: everything is a list expression whose first element is the function/operator and the rest are its args. Following this idea, recently I decided to create my own Lisp such that everything, even `def` are simply functions that update something in the look-up env table. This seemed to work in the beginning when I was using recursive descent to write my interpreter. Using recursive descent seemed like a suitable method to parse the expressions of this Lisp-y language: Any time we see a list of at least two elements, we treat the first as function and parse the rest of elements as args, then we apply the function on the parsed arguments (supposedly, the function exists in the env). But this only gets us so far. What if we now want to have conditionals? Can we simply treat `cond` as a function that treats its args as conditions/consequences? Technically we could, but do you actually want to parse all if/else conditions and consequences, or would you rather stop as soon as one of the conditions turns True? So now we have to introduce a special rule: for `cond`, we don't recursively parse all the args—instead we start parsing and evaluating conditions one by one until one of them is true. Then, and only then, do we parse the associated consequence expression. But this means that `cond` is not a function anymore because it could be that for two different inputs, it returns the same output. For example, suppose the first condition is True, and then replace the rest of the conditions with something else. `cond` still returns the same output even though its input args have changed. So `cond` is not a function anymore! So essentially, my understanding so far is that Lisp has list expressions, but what goes on *inside* those expressions is not necessarily following one unifying syntax rule—it actually follows many rules. And things get more complicated when we throw macros in the mix: Now we have the ability to have literally any syntax within the confines of the list expressions, infinite syntax! And for Lisps like Common Lisp and Racket (not Clojure), you could even have *reader macros* that don't necessarily expect list expressions either. So you could even ,escape the confines of list expressions—even more syntax unlocked! What do you think about this? PS: To be honest, this discovery has made Lisp a bit less "special and magical" for me. Now I treat it like any other language that has many syntax rules, except that for the most part, those rules are simply wrapped and expressed in a rather uniform format (list expressions). But nothing else about Lisp's syntax seems to be special. I still like Lisp, it's just that once you actually want to do computation with a Lisp, you inevitably have to stop the (function args) syntax rule and create new one. It looks like only a pure lambda calculus language implemented in Lisp (...) notation could give us the (function args) unifying syntax.
4 by behnamoh | 3 comments on Hacker News.
A popular notion is that Lisp has no syntax. People also say Lisp's syntax is just the one rule: everything is a list expression whose first element is the function/operator and the rest are its args. Following this idea, recently I decided to create my own Lisp such that everything, even `def` are simply functions that update something in the look-up env table. This seemed to work in the beginning when I was using recursive descent to write my interpreter. Using recursive descent seemed like a suitable method to parse the expressions of this Lisp-y language: Any time we see a list of at least two elements, we treat the first as function and parse the rest of elements as args, then we apply the function on the parsed arguments (supposedly, the function exists in the env). But this only gets us so far. What if we now want to have conditionals? Can we simply treat `cond` as a function that treats its args as conditions/consequences? Technically we could, but do you actually want to parse all if/else conditions and consequences, or would you rather stop as soon as one of the conditions turns True? So now we have to introduce a special rule: for `cond`, we don't recursively parse all the args—instead we start parsing and evaluating conditions one by one until one of them is true. Then, and only then, do we parse the associated consequence expression. But this means that `cond` is not a function anymore because it could be that for two different inputs, it returns the same output. For example, suppose the first condition is True, and then replace the rest of the conditions with something else. `cond` still returns the same output even though its input args have changed. So `cond` is not a function anymore! So essentially, my understanding so far is that Lisp has list expressions, but what goes on *inside* those expressions is not necessarily following one unifying syntax rule—it actually follows many rules. And things get more complicated when we throw macros in the mix: Now we have the ability to have literally any syntax within the confines of the list expressions, infinite syntax! And for Lisps like Common Lisp and Racket (not Clojure), you could even have *reader macros* that don't necessarily expect list expressions either. So you could even ,escape the confines of list expressions—even more syntax unlocked! What do you think about this? PS: To be honest, this discovery has made Lisp a bit less "special and magical" for me. Now I treat it like any other language that has many syntax rules, except that for the most part, those rules are simply wrapped and expressed in a rather uniform format (list expressions). But nothing else about Lisp's syntax seems to be special. I still like Lisp, it's just that once you actually want to do computation with a Lisp, you inevitably have to stop the (function args) syntax rule and create new one. It looks like only a pure lambda calculus language implemented in Lisp (...) notation could give us the (function args) unifying syntax.
Comments
Post a Comment