(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))))
(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)))))
;; ex1.3: 3 item only need compare twice (define (f1 x y z) (+ (if (> x y) (* x x) (* y y)) (if (> x z) (* x x) (* z z)) ) ) ;; This makes bug when the x is largest.:(
(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)))
<< Previous exercise (1.2) | sicp-solutions | Next exercise (1.4) >>