Hacker News new | ask | show | jobs
by kazinator 4109 days ago
But! Perfect splitting across lines according to a formatting algorithm which is simple, consistent, and incrementally applicable:

1.

   (/ (+ (- b) (sqrt (- (* b b) (* 4 a c))))
      (* 2 a))
2.

   (/ (+ (- b)
         (sqrt (- (* b b) (* 4 a c))))
      (* 2 a))
3.

   (/ (+ (- b)
         (sqrt (- (* b b)
                  (* 4 a c))))
      (* 2 a))
4.

   (/ (+ (- b)
         (sqrt (- (* b
                     b)
                  (* 4 
                     a 
                     c))))
      (* 2
         a))
Now you have a sideways tree, revealing the structure of the expression, where it is immediately apparent what the operands are of the / and the + and so on.

Infix turns into a mess breakfast when it's too long for one line.

For this particular expression, I'd probably go with variant (3) in production code. Compared to the beauty of (3), the original one-liner is basically a strawman. In terms of clarity of structure, it trumps the infix also.

This is actually a very important point that is overlooked by Lisp noobs. In real Lisp code, expressions are not written all out in one line, whereby the human reader must mentally match the parentheses. Even numeric expressions that might be one-liners in Fortran or C, are split across several lines to make at least the major constituents clear in relation to the major operator.