summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace/sicp/one/three.scm
blob: 3546fc99218d7b65e00c2ccae9dd7a74e47391da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
;;; Under Creative Commons Attribution-ShareAlike 4.0
;;; International. See
;;; <https://creativecommons.org/licenses/by-sa/4.0/>.

(define-module (net ricketyspace sicp one three)
  #:export (square sum-of-squares largest first-arg-2nd-largest-p
                   sum-of-squares-of-largest-two))

(define (square x) (* x x))

(define (sum-of-squares x y)
  (+ (square x) (square y)))

(define (largest x y z)
  (cond ((and (>= x y)
              (>= x z)) x)
        ((and (>= y x)
              (>= y z)) y)
        ((and (>= z x)
              (>= z y)) z)))

(define (first-arg-2nd-largest-p x y z)
  (or (and (>= x y)
           (<= x z))
      (and (<= x y)
           (>= x z))
      ))

(define (sum-of-squares-of-largest-two x y z)
  (sum-of-squares (largest x y z)
                  (cond ((first-arg-2nd-largest-p x y z)
                         x)
                        ((first-arg-2nd-largest-p y x z)
                         y)
                        ((first-arg-2nd-largest-p z x y)
                         z))))