3 ms·
One of the nice things about Lisp is there are no statements, only expressions. It's easier to think about your code. Lisp was the first language I used (othe
by gumby 4y ago
One of the nice things about Lisp is there are no statements, only expressions. It's easier to think about your code.
Lisp was the first language I used (other than assembly code) so when I encountered C I was puzzled why they even bothered to have statements at all. 40 years later I still don't understand the point.
- zabzonk 4y agoUnderstand where you are coming from, but given the nature of C, what is the compiler to do with something like: 1 2 3 which are three expressions, and probably the programmer meant "123". Of course: 1; 2; 3; is OK.
- gumby 4y agoBut that’s the point: what value does a language gain by making the distinction? It removes composability for no gain.
- zabzonk 4y agoThere is some gain in that you need to explicitly say that something is evaluatable, like: (+ 1 2) as opposed to: 1+2 Whether the gain is worth it is, I agree, debatable.
- gumby 4y agoLisp’s homoiconic representation is not relevant to this issue. In C, `1+2` is an expression that returns 3. `1+2+3` acts like two expressions (and perhaps it is; I forget the precise terminology used in the standard). You can write `return foo(1+2)`. But `for` introduces a statement so can only be used where a statement can.
- goto11 4y agoHaskell is also based on an everything-is-an-expression syntax, but ended up re-inventing statements one level up, in the do-notation blocks. To me, this indicates there is some merit to the statement/expression distinction.
- sixbrx 4y agoI'd say Haskell didn't reinvent statements so much as it just modeled them. That allows manipulating them before they are interpreted, which is a good thing generally.
- S1ngleM4lt 4y agoAs someone who had recently started going the other direction, from C and C-based languages to those more influenced by lisp, it took a bit of getting used to but I’ve grown to appreciate the expressiveness and composability. The only thing I can think of that statements allow, but as I understand it pure expressions don’t, would be early returns. The closest replacement I’ve seen is to just make “return” raise an exception and wrap the whole expression with the try/catch equivalent, which isn’t terrible but was a bit surprising coming from a C-based background where returning early is a fairly common pattern.
- Jtsummers 4y agoYou shouldn't need to raise exceptions for early returns. You can usually restructure your program to provide for the proper return without needing to use a return or comparable form when using an expression oriented language. If you had this C snippet, for instance: int foo(...) { if (condition) return an_expression; ... return another_expression; } You'd put everything after that if into the else part instead of dropping it (like you would in C): (defun foo (...) (if condition an-expression (progn ... another-expression))) ;; and this /progn/ form would possibly be extracted to a new function if it was too long Or more likely a cond if using Common Lisp and there were more than two cases: (defun foo (...) (cond (condition an-expression) ... (t ... another-expression))) And in Common Lisp (and some others) you can still get a return form, using return-from in particular, from functions if you want to keep something more like the C-ish form: (defun foo (...) (when condition (return-from foo an-expression)) ... another-expression))
- S1ngleM4lt 4y agoThanks, learned something new today with return-from! I’d be curious how different cl implementations implement it. The first few examples are what I was more familiar with, and it is precisely your note “and this /progn/ form would possibly be extracted to a new function if it was too long” which makes me want to just return early in some cases. Sometimes a case is common enough that it warrants a separate function, but having to define functions for incredibly specific cases just to avoid excessive indentation is not ideal for me.