Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SICP Exercise 1.3 request for comments

Tags:

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)))))) 
like image 598
ashitaka Avatar asked Oct 02 '08 10:10

ashitaka


1 Answers

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)))) 
like image 168
Carlos Santos Avatar answered Oct 18 '22 22:10

Carlos Santos