6 ms·
I tinkered a bit (as many others have too!) with a lisp syntax for C, just to see what it'd feel like. That is, exact C semantics (so not inventing anything) b
by evmar 8y ago
I tinkered a bit (as many others have too!) with a lisp syntax for C, just to see what it'd feel like. That is, exact C semantics (so not inventing anything) but sexpressions.
What I remember struggling with is that it was surprisingly kind of hard to remember when parens are necessary where. For example you might define an if statement:
(if (> x 3) (printf ...))
Looks nice, but what about if you want multiple things in the body? Your options are
(if (> x 2) (progn (printf a) (return b)))
Where "progn" means "curly braces" effectively, or instead always requiring a list-shaped argument
(if (> x 2) ((printf a) (return b)))
which means in even the single-expression case you have to remember to always double-wrap
(if (> x 2) ((printf a)))
And now throw in handling of 'else' into the above. It gets surprisingly hard to use surprisingly fast. This problem repeats everywhere (function definitions, type declarations, for loops, and so on).
What all the above really clarified for me is that sexps work great in languages like lisp/scheme that are designed around them, but they don't solve syntax for free.
- AnaniasAnanas 8y agoOr "if" could be a special form so that you can just write (if (> x 3) (printf a) (printf b))
- erik_seaberg 8y agoWHEN takes an implicit PROGN like that (so does UNLESS). IF can't because it needs to delimit the then-form from the else-form somehow. http://clhs.lisp.se/Body/m_when_.htm http://clhs.lisp.se/Body/m_when_.htm
- lispm 8y agoSee the Common Lisp IF* macro: (if* (> 3 2) then (foo) (bar) else (baz)) https://franz.com/~jkf/ifstar.txt https://franz.com/~jkf/ifstar.txt
- kazinator 8y ago(when (> x ) (printf a) (return b)) (cond ((> x 3) (printf a) (return b)) ((< x 2) (printf b) (return a)) (t (return c))) Nobody who works with Lisps thinks in terms of "do I need double parentheses". You just know that if takes two or three expressions, when takes an expression followed by zero or more forms, cond takes a sequence of zero or more clauses which consist of a test followed by a body of zero or more forms. If you're designing an S-exp syntax for something else, it's probably best to keep the familiar things the same; don't make some different if and such. The Lisp if maps naturally to the ?: ternary operator; cond, when and unless can compile to cascaded if/else if/else.
- evmar 8y agoYes, but you are describing how a lisp programmer would expect things to work. As my comment was saying, I was exploring whether a verbatim translation of C syntax works as sexeps, and where it fails.