sicp-ex-1.3

``` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Exercise 1.3.  Define a procedure that takes three numbers as arguments
;; and returns the sum of the squares of the two larger numbers.
;;
;; In short, I did this starting with the pseudo code procedure
;; to accomplish the task at hand then built the helpers needed
;; to make it work. The only thing in this example that is out
;; of sync with the thought process was the >= and <= procedures
;; that I added after wrestling with too many parenthesis.

(define (exercise1.3 x y z)
(sum (sqr (first x y z))
(sqr (second x y z))))

(define (>= x y)
(not (< x y)))

(define (<= x y)
(not (> x y)))

(define (first x y z)
(cond ((and (>= x y) (>= x z)) x)
((and (>= y x) (>= y z)) y)
(else z)))

(define (second x y z)
(cond ((or (and (<= x y) (>= x z))
(and (>= x y) (<= x z))) x)
((or (and (<= y x) (>= y z))
(and (>= y x) (<= y z))) y)
(else z)))

(define (sqr x)
(* x x))

(define (sum x y)
(+ x y))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The whole thing put together:
;; Added 'not's to eliminate all helper procedures

(define (exercise1.3 x y z)
(+ (* (cond ((and (not (< x y)) (not (< x z))) x)
((and (not (< y x)) (not (< y z))) y)
(else z))

(cond ((and (not (< x y)) (not (< x z))) x)
((and (not (< y x)) (not (< y z))) y)
(else z)))

(* (cond ((or (and (not (> x y)) (not (< x z)))
(and (not (< x y)) (not (> x z)))) x)
((or (and (not (> y x)) (not (< y z)))
(and (not (< y x)) (not (> y z)))) y)
(else z))

(cond ((or (and (not (> x y)) (not (< x z)))
(and (not (< x y)) (not (> x z)))) x)
((or (and (not (> y x)) (not (< y z)))
(and (not (< y x)) (not (> y z)))) y)
(else z)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A non-elegant, brutish solution to be sure. But its all mine.

```
```
(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)))))

```
```
(define (square x) (* x x))
(define (exam x y z)
(if (or (> x y) (> x z))
(if (> y z) (+ (square x) (square y))
(+ (square x) (square z)))
(+ (square y) (square z))))

```
``` ;; Different approach, where square of the lowest term is deducted.
(define (f a b c) (- (+ (* a a) (* b b) (* c c))
(* (cond ((and (< a b) (< a c)) (* a a))
((and (< b c) (< b a)) (* b b))
(else (* c c))))))

```
``` ;; Using sorting networks

(define (max a b)
(cond ((< a b) b)
(else a))
)

(define (min a b)
(cond ((> a b) b)
(else a))
)

(define (square a)
(* a a)
)

(define (sorting-network-3 a b c)
(let* ((stage1-b (max b c)) (stage1-c (min b c)) (stage2-a (max a stage1-c)))
(list (max stage2-a stage1-b) (min stage2-a stage1-b) (min a stage1-c))
)
)

(define (ex13 a b c)
(let* ((sorted (sorting-network-3 a b c)) (maximal (first sorted)) (middle (second sorted)))
(+ (square maximal) (square middle))
)
)

```
``` ;; Doesn't use helper functions.

(define (ex13 x y z)
(+ (if (or (>= x y) (>= x z)) (* x x) 0)
(if (or (> y x) (>= y z)) (* y y) 0)
(if (or (> z x) (> z y)) (* z z) 0)))

```
``` ;; Uses two auxiliary procedures.

(define (largest x y)
(if (> x y)
x
y))

(define (smallest x y)
(if (< x y)
x
y))

(define (sum_of_two_largest a b c)
(define largest_a (largest a b))
(define largest_b (largest (smallest a b) c))
(+ (square largest_a) (square largest_b)))

(sum_of_two_largest 4 5 6)
; Should be 61
```
``` ;; uses a play of max and min of pairs to get the
;; two max numbers of a triple

(define (max x y)
(if (> x y)
x
y))

(define (min x y)
(if (< x y)
x
y))

;; sum of squares
(define (sos x y) (+ (square x) (square y)))

;; sum of squares of the two max numbers in a triple
(define (sos_two_max x y z)
(sos (max x y) (max (min x y) z)))

(sos_two_max 3 2 1)
; result should be 13
```

```
```