11 ms·
Personally I find that LISP syntax remove a layer of complexity by directly exposing the AST to my brain instead of adding a layer of internal parsing. 1 +
by yogsototh 3y ago
Personally I find that LISP syntax remove a layer of complexity by directly exposing the AST to my brain instead of adding a layer of internal parsing.
1 + x * 2 - 3 % x
is longer to decipher than
(% (- (+ (* x 2) 1) 3) x)
which is itself harder than
(-> x (* 2) (+ 1) (- 3) (% x))
But it takes a while to be used to it.
And yes, it really helps writing macros, but I wouldn't say this as always be a good thing. Macros are far from being the alpha and omega of programming, as they add an implicit layer of transformation to your code making it easier to write but very often harder to read and reason about.
- sbergot 3y agoYou left out the more common options: (1 + x * 2 - 3) % x and (1 + (x * 2) - 3) % x both are clearer than the S-expression in my opinion
- Paul-Craft 3y agoI agree, and this is why for math expressions that aren't just composition of functions that aren't basic operators, I like to use a macro that lets me type them in as infix. It's the one case where lispy syntax just doesn't work well, IMO.
- zelphirkalt 3y agoI don't find them to be clearer, with the background of knowing many language, because now I have to worry about precedence and I better double check, to not get it wrong or read it wrong.
- digdugdirk 3y agoAs someone who isn't a trained programmer (and has no background or understanding of lisp) that looks like you took something sensible and turned it into gibberish. Is there a recommended "intro to understanding lisp" resource out there for someone like myself to dive in to?
- _aaed 3y agohttps://en.wikipedia.org/wiki/Polish_notation#Explanation https://en.wikipedia.org/wiki/Polish_notation#Explanation
- teamonkey 3y ago'( 1. This is a list: ( ). Everything is a list. Data structures are all lists. 2. A program is a list of function calls 3. Function calls are lists of instructions and parameters. For (A B C), A is the function name, B and C are parameters. 4. If you don't want to execute a list as a function, but as data, you 'quote' it using a single quote mark '(A B C) 5. Data is code, code is data.)
- Capricorn2481 3y ago(-> x (* 2) (+ 1) (- 3) (% x)) The part that is confusing if you don't know Clojure is (->). This a thread macro, and it passes "x" through a list of functions. So it basically breaks this down into a list of instructions to do to x. You will multiply it by 2, add 1 to it, take 3 from it, then do the modulus by the original value of x (the value before any of these steps). Clojurists feel like this looks more readable than the alternative, because you have a list of transformations to read left to right, vs this (% (- (+ (* x 2) 1) 3) x) Which is the most unreadable of them all, to me.
- fungiblecog 3y agothis is very readable if you know to read the operations inside-out instead of left-to-right
- ziml77 3y agoBut that reading requires looking back and forth to read the operator and the operand. The further you move out the more you shift your eyes and the harder it becomes to quickly jump back to the level of nesting that you are currently on at the other side.
- Capricorn2481 3y ago
- perrygeo 3y agoI started working through Crafting Interpreters, building up a language syntax and grammar from scratch. A lot of work and 75 pages of lex/parse logic and we now have a AST... that we can debug and inspect by looking directly at its sexp representation. It was the ah-ha moment for me... why not express the source cost directly as that AST? Most languages require lots of ceremony and custom rules just to get here. Sexps are a step ahead (inherently simpler) since they're already parsable as an unambiguous tree structure. It's hard to unsee - reading any non-Lisp language now feels like an additional layer of complexity hiding the real logic.
- packetlost 3y agoThose rules help reduce runtime surprises though, to be fair. It's not like they exist for not purpose. It directly represents the language designer making decisions to limit what is a valid representation in that language. Rule #1 of building robust systems is making invalid state unrepresentable, and that's exactly what a lot of languages aim to do.
- munificent 3y agoMuch of the complexity and error reporting that exists in the lexer or parser in a non-Lisp language just gets kicked down the road to a later phase in a Lisp. Sure, s-exprs are much easier to parse. But the compiler or runtime still needs to report an error when you have an s-expr that is syntactically valid but semantically wrong like: (let ()) (1 + 2) (define) Kicking that down the road is a feature because it lets macros operate at a point in time before that validation has occurred. This means they can accept as input s-exprs that are not semantically valid but will become after macro expansion. But it can be a bug because it means later phases in the compiler and runtime have to do more sanity checking and program validation is woven throughout the entire system. Also, the definition of what "valid" code is for human readers becomes fuzzier.
- HelloNurse 3y ago> later phases in the compiler and runtime have to do more sanity checking But they always have to do all the sanity checking they need, because earlier compiler stages might introduce errors and propagate errors they neglect to check. > program validation is woven throughout the entire system Also normal and unavoidable. As far as processing has logical phases and layers, validation aligns with those layers (the compiler driver ensures that input files can be read and have the proper text encoding, the more language-specific lexer detects mismatched delimiters and unrecognized keywords, and so on); combining phases, e.g. building a symbol table on the go to detect unidentified identifiers before parsing is complete, is a deliberate choice to improve performance but increase complication.
- agumonkey 3y agoThat's how my brain feels. it connects informations (compound terms) to entities directly, it's almost minimized information required to represent something, unlike algol based languages.
- mlajtos 3y agoReject PEMDAS, return to monky: x * 2 + 1 - 3 % x https://mlajtos.mu/posts/new-kind-of-paper-2 https://mlajtos.mu/posts/new-kind-of-paper-2
- rileyphone 3y agoMy preferred version of this: x.*(2).+(1).-(3).%(x) Unfortunately our brains are broken by pemdas and need clear delineations to not get confused; this syntax also extends to multiple arguments and is amenable to nesting.
- Tainnor 3y agoWell, the easiest way to write 1 + x * 2 - 3 % x would just be "x-2". But if we're talking more generally, if I have an expression like 2*x^3 + x^2 - 5*x a trained eye immediately can read off the coefficients of the polynomial and I'm not sure if that's true of (+ (* 2 (^ x 3)) (^ x 2) (- (* 5 x)))
- zelphirkalt 3y agoFor writing a program, the s-expression form might become: (+ (* 2 (^ x 3)) (^ x 2) (- (* 5 x))) Whereas: 2*x^3 + x^2 - 5*x Would probably error out in most languages, due to parsing issues and ambiguity. Even worse ambiguity, if you put the signs in front, as then every line could be an expression by itself: 2*x^3 + x^2 - 5*x Could be 3 expressions or 2 or 1.
- Tainnor 3y agoIt might do the wrong thing in some languages but wouldn't necessarily raise a compiler error, and I'm fairly certain e.g. sympy should have no issue with it.
- todd8 3y agoWhen I learned APL, the expression evaluation order at first seemed odd (strictly right to left with no operator presence, 5*5+4 evals to 45 not 29). After working with it a couple of hours I came to appreciate its simplicity, kind of like the thread operator in your last example.
- mmphosis 3y agoI prefer infix over prefix. And don't forget postfix x 3 1 2 x * + - % I definitely prefer white-space over commas and other syntactic obstacle courses.
- Tyr42 3y agoI mean I would probably punch that in as x 2 * 1 + (-) 3 + x % When I was using my rpn calculator. (-) flips the sign of the number on top of the stack. Or use flip -. But you can avoid growing the stack long enough to be confusing.
- kazinator 3y agoUsing piping for arithmetic is reminscent of grade school arithmetic dictations. "Start with 10; add 5; divide by 3; ..." :)
- drBonkers 3y ago> `(-> x (* 2) (+ 1) (- 3) (% x))` Love the single pipe operator. What language works this way?
- Folcon 3y agoThat's a clojure norm, it may exist in other lisps.
- rpz 3y agoIf the language has no operator precedence then 1+x*2-3%x is just as easy if not easier to decipher compared to both of your other examples imo. The above is equivalent to (1+(x*(2-(3%x)))) in APL/j/k. You get used to it pretty quickly.