I'm trying to learn scheme via SICP. Exercise 1.3 reads as follow: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. Please comment on how I can improve my solution.
(define (big x y) (if (> x y) x y)) (define (p a b c) (cond ((> a b) (+ (square a) (square (big b c)))) (else (+ (square b) (square (big a c))))))
Using only the concepts presented at that point of the book, I would do it:
(define (square x) (* x x)) (define (sum-of-squares x y) (+ (square x) (square y))) (define (min x y) (if (< x y) x y)) (define (max x y) (if (> x y) x y)) (define (sum-squares-2-biggest x y z) (sum-of-squares (max x y) (max z (min x y))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With