by casualrandomcom · 4y ago · 0 comments
(/ (+ (- b) (sqrt (- (* b b) (* 4 (* a c)))) (* 2 a))
is a pain in the neck, and even more so when you have to read it, while
(-b + sqrt(b*b - 4*a*c))/(2*a),
can be checked on the fly.
I am kind of amazed this hasn't finally sunk into programmers' hearts.
I can write ugly code in any language. But if I was writing this code in Clojure, I would probably do something like this:
(let [r1 (- (* b b) (* 4 a c))
r2 (+ (- b) (sqrt r1)))
r3 (* 2 a)]
(/ r2 r3))
That is perfectly readable to me.Yeah, you may say that I needed to introduce other variables to make this work and the code got bigger as a result, but on the other side how many people are actually writing this kinda of code on daily basis that are not Computer Scientists?
[1]: https://github.com/gilch/hissp
[2]: https://hissp.readthedocs.io/en/v0.3.0/tutorial.html#hello-w...
(py "(-b + sqrt(b*b - 4*a*c))/(2*a)") (/ (+ (- b)
(sqrt (- (* b b)
(* 4 (* a c))))
(* 2 a))
The trick is that a trail of any number of `)`'s should also end the line. Like any language, it takes some getting used to, but I find this tree structure perfectly readable now.Sure for the four-function calculator operations, operator precedence levels are a reasonable notation, but when you can define your own functions, and your programming language now needs a dozen precedence levels, it becomes hard to read. Lisp is completely unambiguous.