-
Notifications
You must be signed in to change notification settings - Fork 1
/
chapter-00.rkt
74 lines (58 loc) · 1.16 KB
/
chapter-00.rkt
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#lang racket
;; Frame 6
(define pie 3.14)
;; Frame 7
(define a-radius 8.4)
(define an-area (* pie (* a-radius a-radius)))
;; Frame 10
(define area-of-circle
(λ (r)
(* pie (* r r))))
;; Frame 12
(define area-of-rectangle
(λ (width)
(λ (height)
(* width height))))
;; Frame 14
(define double-result-of-f
(λ (f)
(λ (z)
(* 2 (f z)))))
;; Frame 15
(define add3
(λ (x)
(+ 3 x)))
;; Frame 24
(define abs
(λ (x)
(cond
[(< x 0) (- 0 x)]
[else x])))
;; Frame 26
(define silly-abs
(λ (x)
(let ([x-is-negative (< x 0)])
(cond
[x-is-negative (- 0 x)]
[else x]))))
;; Frame 37
(define remainder
(λ (x y)
(cond
[(< x y) x]
[else (remainder (- x y) y)])))
;; Frame 49
;; This is slightly different from the book, as I feel it is more clear.
(define add
(λ (n m)
(cond
[(zero? m) n]
[else (add (add1 n) (sub1 m))])))
(module+ test
(require rackunit)
(check-= an-area 221.5584 0.0001)
(check-= an-area (area-of-circle a-radius) 0.0001)
(check-equal? ((double-result-of-f add3) 4) 14)
(check-equal? (remainder 13 4) 1)
(check-equal? (add 7 2) 9)
)