]> git.rkrishnan.org Git - sicp.git/blob - src/sicp/ex1_3.clj
Solution to 4.33. This had been difficult to get right, though conceptually it was
[sicp.git] / src / sicp / ex1_3.clj
1 (ns sicp.ex1_3
2   (:use [sicp utils]
3         [clojure.contrib trace test-is]))
4
5 ;; exercise 1.3:  Define a procedure that takes three numbers as
6 ;;                arguments and returns the sum of the squares of
7 ;;                the two larger numbers.
8 (defn sort3 [a b c]
9   (cond (> b a) (sort3 b a c)
10         (< b c) (sort3 a c b)
11         :else [a b c]))
12
13 (defn sum-of-sq-of-two-largest [a b c]
14   (apply sum-of-squares (take 2 (sort3 a b c))))
15
16 ;; well, I cheated above. Let me use only the constructs introduced
17 ;; so far in the book. (follows after the sicp meetup #2 on 28/mar/2010.
18 (defn sum-of-square-of-two-largest [a b c]
19   (if (> a b)
20     (if (> b c)
21       (sum-of-squares a b) ; a > b > c
22       (sum-of-squares a c))
23     (if (> a c)
24       (sum-of-squares b a)
25       (sum-of-squares b c))))