2 ms·
Infix notation can be useful for arithmetic; as a "Lisp person" I acknowledge that. S-exps can be good for arithmetic too though. S-expressions allow n-ary op
by kazinator 4y ago
Infix notation can be useful for arithmetic; as a "Lisp person" I acknowledge that.
S-exps can be good for arithmetic too though.
S-expressions allow n-ary operators:
(+ (* 3 x y) (* a b z w) (* r s t u v))
There is a systematic way to break up large expressions across multiple lines, which helps readability. Infix doesn't have a satisfactory way to do this:
(+ (* 3 x y)
(* a b z w)
(* r s t
u v))
We see just from from the
+ *
*
*
pattern that we have a sum of three products.
S-exps are unambiguous. You never misread an arithmetic (or any other) S-exp due to precedence, unless it's a macro that has created precedence rules among its arguments (vanishingly rare).
N-ary math functions associate left to right in Common LIsp: (+ a b c d) means we add (a b) first, then c, and d. This makes a difference when you have mixed types, which have to undergo conversions, and when you have non-associative types like floating-point.
If I'm cribbing some numeric calculation to use in my code (whether that code is Lisp or not), and have examples in half a dozen languages, none of which I use, I will crib it from that one that is a Lisp dialect, just because of the vanishingly small opportunity for getting it wrong due to some ambiguity issue.
- deleted 4y ago[deleted]
- jstimpfle 4y agoThis is a comment that I like, acknowledging the obvious unsophisticated but no less reasonable objection from the bystanders, and offering them an alternate viewpoint. My reservation would be that most applications of arithmetic operators are binary and not ternary. A typical expression in one of my C programs at least has some function call, some subscript, some member access, some addition / subtraction / multiplication / division. But very few have more than one of each. Overall arithmetic is actually quite rare by itself in most code.