summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace/sicp/one/seventeen.scm
blob: 1ef69ccd8da7b513a7f79e67ba40f6d83841970e (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
37
38
39
40
41
42
43
44
45
46
47
48
49

;;;; Under Creative Commons Attribution-ShareAlike 4.0
;;;; International. See
;;;; <https://creativecommons.org/licenses/by-sa/4.0/>.

;;;
;;;  __________________
;;; < +, - everything. >
;;;  ------------------
;;;   \            .    .     .
;;;    \      .  . .     `  ,
;;;     \    .; .  : .' :  :  : .
;;;      \   i..`: i` i.i.,i  i .
;;;       \   `,--.|i |i|ii|ii|i:
;;;            U**U\.'@@@@@@`.||'
;;;            \__/(@@@@@@@@@@)'
;;;                 (@@@@@@@@)
;;;                 `YY~~~~YY'
;;;                  ||    ||
;;;

(define-module (net ricketyspace sicp one seventeen)
  #:export (double
            halve
            fast-*)
  #:replace (even?))

(define (double a)
  "Return A + A."
  (+ a a))

(define (halve b)
  "Return B/2.

B must be an even number."
  (cond ((= b 0) 0)
        (else (+ 1 (halve (- b 2))))))

(define (even? b)
  "Return #t if B is divisible by 2; #f otherwise."
  (cond ((= b 1) #f)
        ((= b 0) #t)
        (else (even? (- b 2)))))

(define (fast-* a b)
  "Return A * B."
  (cond ((= b 0) 0)
        ((even? b) (double (fast-* a (halve b))))
        (else (+ a (fast-* a (- b 1))))))