Define a procedure that takes three numbers as arguments and returns the sum of squares of the two largest numbers.
I'm using just the machinery that was developed so far in SICP to be true to the exercise. My method works as far as I can tell -- I tested it for a number of cases and tested different permutations of inputs and get the same answers in each case. I just wasn't sure if there were a better way to do this exercise. Any feedback whatsoever is welcomed.
;; Define the squaring function. (define (square n) (* n n) ) ;; Define the maximum of two inputs a and b. (define (mx a b) (cond ((< a b) b) ((< b a) a) ((= a b) a)) ) ;; Define the constant of elimination. (define (coe x y z) (cond ((= x y) (-(square x))) ((= y z) (-(square y))) ((= x z) (-(square z)))) ) ;; Define the sum of squares function. It consists of the sum of squares of the maxima of ;; each of three possible pairs of three elements. Two of the first three terms will always ;; evaluate to the same value, and so the constant of elimination is added to get rid of the redundant term. (define (sosl a b c) (+ (square (mx a b)) (square (mx b c)) (square (mx a c)) (coe (mx a b) (mx b c) (mx a c)) ) )