sicp-ex-1.3


  
 (define  
     (largest-two-square-sum x y z)  
         (if (= x (larger x y))  
             (sum-of-squares x (larger y z))  
             (sum-of-squares y (larger x z)) 
         ) 
 ) 
  
 (define  
     (larger x y)  
         (if (> x y) x y) 
 ) 
  
 (define  
     (sum-of-squares x y)  
         (+ (square x) (square y)) 
 ) 
  
 (define  
     (square x)  
         (* x x) 
 ) 
  
  
 ;;  ex1.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 (sum-of-squared-largest-two x y z) 
         (cond ((= (min x y z) x) (sum-of-squares y z)) 
               ((= (min x y z) y) (sum-of-squares x z)) 
               ((= (min x y z) z) (sum-of-squares x y)))) 
  
 ;; Testing 
 (sum-of-squared-largest-two 1 3 4) 
 (sum-of-squared-largest-two 4 1 3) 
 (sum-of-squared-largest-two 3 4 1) 
  
  
 ;; ex 1.3 
 ;; implemented using only techniques covered to this point 
  
 (define (square x) (* x x)) 
  
 (define (sum-of-squares x y) 
   (+ (square x) (square y))) 
  
 (define (largest-two-of-three x y z) 
   (if (>= x y) 
       (sum-of-squares x (if (>= y z) y z)) 
       (sum-of-squares y (if (>= x z) x z)))) 
  
 ;; tests 
 (largest-two-of-three 2 3 4) 
 (largest-two-of-three 4 2 3) 
 (largest-two-of-three 3 4 2) 
  
 (define (smallest-two-of-three a b c) 
   (if (< a b)  
     (if (< a c) a c) 
     (if (< b c) b c))) 
  
 (define (square a) 
   (* a a)) 
  
 (define (sum-of-squares-largest-two-of-three a b c)  
   (+ (square a) (square b) (square c) (- (square (smallest-two-of-three a b c))))) 
 (define (sum-of-squares x y) 
   (+ (* x x) (* y y))) 
  
 (define (sum-of-squares-of-two-largest a b c) 
   (cond ((and (<= a b) (<= a c)) (sum-of-squares b c)) 
         ((<= a b) (sum-of-squares a b)) 
         (else (sum-of-squares a c)))) 
 ;; wrong when (and (> a b) (> b c)) 
 (define (sum-of-squared-largest-two a b c) 
   (- (+ (square a) (square b) (square c)) (square (min a b c)))) 
 ;; a recursive solution 
  
 (define (sum-of-squares-of-two-largest a b c) 
   (define (>= x y) 
     (or (> x y) (= x y))) 
   (define (square x) 
     (* x x)) 
   (cond ((and (>= a b) (>= b c)) (+ (square a) (square b))) ; a >= b >= c 
         ((and (>= b a) (>= c b)) (+ (square b) (square c))) ; a <= b <= c 
         (else (sum-of-squares-of-two-largest b c a)))) 
 ;; Exercise 1.3 
 ;; Uses no helper functions 
  
 (define (sumsqrs-largest x y z) 
         (cond 
                 ((and (>= x z) (>= y z)) (+ (* x x) (* y y))) 
                 ((and (>= x y) (>= z y)) (+ (* x x) (* z z))) 
                 ((and (>= y x) (>= z x)) (+ (* y y) (* z z))))) 
 ;;  ex1.3: does not implement helper functions nor techniques yet covered in book 
  
 (define (sum-max2-sqr a b c)   
   (cond ((and (not (> c a)) (not (> c b)))  (+ (* a a) (* b b)))         
         ((and (not (> b a)) (not (> b c)))  (+ (* a a) (* c c)))         
         ((and (not (> a b)) (not (> a c)))  (+ (* b b) (* c c)))    
    ) 
 ) 
  
 ;;  testing 
 (sum-max2-sqr 3 4 5) 
 (sum-max2-sqr 3 5 4) 
 (sum-max2-sqr 5 3 4) 
 (sum-max2-sqr 5 4 3) 
 (sum-max2-sqr 4 3 5) 
 (sum-max2-sqr 4 5 3) 
  
 (sum-max2-sqr -1 -2 -3) 
 (sum-max2-sqr -3 -2 -1) 
  
 (sum-max2-sqr 0 0 -1) 
 (sum-max2-sqr 1 0 -1) 
  
 (sum-max2-sqr 3 3 5) 
 (sum-max2-sqr 3 5 5) 
  
 ;; ex1.3: Another variation not used yet 
  
 (define (if-you-want-to-get-the-sum-of-two-numbers-where-those-two-numbers-are-chosen-by-finding-the-largest-of-two-out-of-three-numbers-and-squaring-them-which-is-multiplying-them-by-itself-then-you-should-input-three-numbers-into-this-function-and-it-will-do-that-for-you x y z) 
   (if (>= x y) 
       (if (>= y z) 
           (+ (* x x) (* y y)) 
           (+ (* x x) (* z z))) 
       (if (>= x z) 
           (+ (* y y) (* x x)) 
           (+ (* y y) (* z z))))) 
  
 (define (largest-two-square-sum x y z) 
   (sum-of-squares (mid x y z) (biggest x y z))) 
  
 (define (sum-of-squares x y) 
   (+ (square x) (square y))) 
  
 (define (square x) 
   (* x x)) 
  
 (define (bigger x y) 
   (if (> x y) x 
       y)) 
  
 (define (biggest x y z) 
   (if (> (bigger x y) z) (bigger x y) 
       z)) 
  
 (define (mini x y) 
   (if (> x y) y 
       x)) 
  
 (define (mid x y z) 
   (bigger (mini x y)  
           (mini (bigger x y) z))) 
  
 ;; no min used 
  
 (define (square x) (* x x)) 
 (define (sum-of-squares x y) (+ (square x) (square y))) 
 (define (biggest x y) (if (> x y) x y)) 
 (define (input x y z) (sum-of-squares (biggest x y) (biggest y z))) 
  
 ;; output 
  
 > (input 1 2 3) 
 13 
 > (input 5 6 7) 
 85 
 > (input 9 10 11) 
 221 
  
 ;; gives wrong answer when (and (> y x) (> y z)) 
  
 (define (sum-of-squares x y z)  
   (+ (square (max x y)) (square (max (min x y) z)))) 
  
 (define (square x) (* x x)) 
  
 ;; test  
 > (sum-of-squares 1 2 3) 
 13 
 > (sum-of-squares 3 2 1) 
 13 
 > (sum-of-squares 3 1 2) 
 13 
 > (sum-of-squares 2 3 1) 
 13 
 > (sum-of-squares 1 3 2) 
 13 
 > (sum-of-squares 1 3 2) 
 13 
 > (sum-of-squares 2 2 1) 
 8 
 > (sum-of-squares 1 2 2) 
 8 
 > (sum-of-squares 2 2 2) 
 8 
  
  
 ;; make it clearly 
  
 (define (square x) (* x x)) 
 (define (larger x y) (if (> x y) x y)) 
 (define (smaller x y) (if (< x y) x y)) 
 (define (largest-two-square-sum x y z) (+ (square (larger x y)) (square (smaller x y)))) 
  
 ;; output 
  
 > (largest-two-square-sum 4 5 6) 
 41 
 ;; this program is wrong, answer should be 61. 
  
 ;; drop thru' cond 
  
 (define (fun x y z) 
   (define (square x) (* x x)) 
   (define (sumsq x y) (+ (square x) (square y))) 
   (cond ((>= x y) (sumsq x (if (>= y z) y z))) 
         ((>= x z) (sumsq x y)) 
         (else (sumsq z y)) 
         )  
   ) 
  
 (define (square a) 
       (* a a)) 
  
 (define (square-sum a b) 
             (+ (square a) (square b))) 
  
 (define (compare a b c) 
            (if (> a b) 
                (if (> b c) (square-sum a b) (square-sum a c)) 
                (if (> a c) (square-sum a b) (square-sum b c)))) 
  
 (define (square a) 
       (* a a)) 
  
 (define (sum-of-squares a b) 
             (+ (square a) (square b))) 
  
 (define (sum-sq-max2 a b c) 
   (cond 
     ((and (< a b) (< a c)) (sum-of-squares b c)) 
     ((and (< b a) (< b c)) (sum-of-squares a c)) 
     (else                  (sum-of-squares a b)) 
   ) 
 ) 
  
 (define (sumsq a b c) 
   (define sum (+ a b c)) 
   (define x (max a b c)) 
   (define y (- sum x (min a b c))) 
   (sum-of-squares x y) 
 ) 
  
  
  
 ;; Doesn't use helper functions. A bit neater than sum-max2-sqr defined above. 
  
 (define (sum a b c) 
         (cond ((not (or (< b a) (< c a))) (+ (* b b) (* c c))) 
               ((not (or (< a b) (< c b))) (+ (* a a) (* c c))) 
               ((not (or (< a c) (< b c))) (+ (* a a) (* b b))))) 
  
  

<< Previous exercise (1.2) | sicp-solutions | Next exercise (1.4) >>