(defun solve-quadratic (a b c)
(let ((delta (- (* b b) (* 4 a c))))
(cond
((< delta 0.0) 'no-solutions)
(t (let ((x1 (/ (- (- 0 b) (sqrt delta)) (* 2 a)))
(x2 (/ (+ (- 0 b) (sqrt delta)) (* 2 a))))
(format t "x1 = ~d, x2 = ~d~%" x1 x2))))))
If the same code were mangled up like so, say:
(defun solve-quadratic (a b c)
(let ((delta (- (* b b) (* 4 a c))))
(cond ((< delta 0.0) 'no-solutions)
(t (let ((x1 (/ (- (- 0 b) (sqrt delta)) (* 2 a)))
(x2 (/ (+ (- 0 b) (sqrt delta)) (* 2 a))))
(format t "x1 = ~d, x2 = ~d~%" x1 x2))))))
Even this becomes much harder to read since we cannot inherently use the indentation to figure our the "shape" of the code. Now imagine you had a missing parenthesis in there, and had no access to any formatter (just for the sake of argument), matching up the parens manually would be a much harder task.
Whereas even if you have very badly manged code in an C-like language:
private static void solveQuadratic(int a, int b, int c) {
double delta = b * b - 4 * a * c;
if (delta < 0.0) {System.out.println("No solutions");
} else { double x1 = (-b - Math.sqrt(delta)) / (2.0 * a);double x2 = (-b + Math.sqrt(delta)) / (2.0 * a);
System.out.printf("x1 = %f, x2 = %f\n", x1, x2);}}
This is still trivial to read. Also, since parentheses and braces are not significant as in the case of Lisps, you could get away with a lot of excess brackets without having to worry about it. With Lisps, parens are not optional - they must be exact. This creates its own problems when refactoring code (yes, even with paredit et al).
Indeed syntax can both enhance and hinder your ability to write readable code.
Look at the following example and tell me if you have any idea about what it does.
solveQuad :: Float -> Float -> Float -> Maybe (Float, Float)
solveQuad a b c = if delta < 0.0 then Nothing else Just (x1, x2)
where
delta = b ^ 2 - 4 * a * c
x1 = -b - (sqrt delta) / (2 * a)
x2 = -b + (sqrt delta) / (2 * a)
-22
u/[deleted] Apr 20 '21
It doesn't feel that good when you try to read.