Dan's Thoughts Thinking somewhat carefully

14May/090

Exercise 1.3 of SICP

Exercise 1.3: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

(define (square x) (* x x))
(define (sum-of-squares x y) (+ (square x) (square y)))
(define (max a b) (if (> a b) a b))
(define (take-two a b c)
  (cond ((and (> a b) (> a c))
         (sum-of-squares a (max b c)))
        ((and (> b a) (> b c))
         (sum-of-squares b (max a c)))
        (else (sum-of-squares c (max a b)))))

Just for fun another way:

(define (square x) (* x x))
(define (sum-of-squares a b c) (+ (square a) (square b) (square c)))
(define (least a b) (if (< a b) a b))
(define (take-two a b c) 
  (- (sum-of-squares a b c) 
     (square (least (least a b) c))))
Filed under: lisp, SICP Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

Spam protection by WP Captcha-Free

No trackbacks yet.