Using circuit code to solve a quadratic equation?

I wrote this circuit code to calculate one solution of the quadratic equation ax2 + bx + c = 0

(define (solve-quadratic-equation a b c) (define disc (sqrt (- (* b b) (* 4.0 a c)))) (/ (+ (- b) disc) (* 2.0 a)))

However, someone told me that this procedure is difficult to understand. Why?

What would a cleaned version of this procedure look like? Please let me know why the new procedure is easier to understand.

thank

+3
source share
2 answers

Well, one reason is that it's all on the same line. You can make it more readable by using something called beautiful printing , where you break it up into several lines and use white space characters:

(define (solve-quadratic-equation a b c)
  (define disc (sqrt (- (* b b)
                        (* 4.0 a c))))
  (/ (+ (- b) disc)
     (* 2.0 a)))

Thus, you can more clearly see the structure of expressions.

And here is a quote from SICP :

( ) , Lisp. , ,

(+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))

57. ,

(+ (* 3
      (+ (* 2 4)
         (+ 3 5)))
   (+ (- 10 7)
      6))

, "-", , . .

+7

? , , .

 (define (solve-quadratic-equation a b c)
     (define square (x) (* x x) 
     (define disc (sqrt (- (square b) (* 4.0 a c)))) 
                        (/ (+ (- b) disc) (* 2.0 a))))
+1

Source: https://habr.com/ru/post/1698910/


All Articles