-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchapter31.rkt
161 lines (129 loc) · 3.87 KB
/
chapter31.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#lang racket
(require test-engine/racket-tests)
(define (invert lst)
(define (invert-acc xs acc)
(cond
[(empty? xs) acc]
[else (invert-acc (rest xs) (cons (first xs) acc))]))
(invert-acc lst empty))
(check-expect (invert (list 1 2 3)) (list 3 2 1))
(check-expect (invert (list )) (list ))
(define (sum xs)
(cond
[(empty? xs) 0]
[else (+ (first xs) (sum (rest xs)))]))
(check-expect (sum (list 1 2 3)) 6)
(check-expect (sum (list )) 0)
(define (sum2 xs)
(define (sum-acc xs acc)
(cond
[(empty? xs) acc]
[else (sum-acc (rest xs) (+ acc (first xs)))]))
(sum-acc xs 0))
(check-expect (sum2 (list 1 2 3)) 6)
(check-expect (sum2 (list )) 0)
;; Exercise 31.3.1
(define (g-series n)
(cond
[(zero? n) empty]
[else (cons (expt -0.99 n) (g-series (sub1 n)))]))
(check-within (sum (g-series #i1000)) -0.49746596003269394 0.001)
(check-within (sum2 (g-series #i1000)) -0.49746596003269533 0.001)
(check-within (* 10e15 (sum (g-series #i1000))) -4974659600326939.0 0.001)
(check-within (* 10e15 (sum2 (g-series #i1000))) -4974659600326953.0 0.001)
(define (! n)
(cond
[(zero? n) 1]
[else (* n (! (sub1 n)))]))
(check-expect (! 3) 6)
(check-expect (! 5) 120)
(define (!2 n)
(define (helper n acc)
(cond
[(zero? n) acc]
[else (helper (sub1 n) (* n acc))]))
(helper n 1))
(check-expect (!2 3) 6)
(check-expect (!2 5) 120)
(struct node (left right))
(define (height tree)
(cond
[(empty? tree) 0]
[else (+ 1 (max (height (node-left tree)) (height (node-right tree))))]))
(define (height2 tree)
(define (helper tree acc)
(cond
[(empty? tree) acc]
[else (max (helper (node-left tree) (add1 acc))
(helper (node-right tree) (add1 acc)))]))
(helper tree 0))
;; Exercise 31.3.3
(define (product lon)
(define (product/acc lon acc)
(cond
[(empty? lon) acc]
[else (product/acc (rest lon) (* acc (first lon)))]))
(product/acc lon 1))
(check-expect (product (list 1 2 3 4 5)) 120)
;; Exercise 31.3.4
(define (how-many xs)
(define (how-many/acc xs acc)
(cond
[(empty? xs) acc]
[else (how-many/acc (rest xs) (add1 acc))]))
(how-many/acc xs 0))
(check-expect (how-many (list 1 2 3 4 5)) 5)
(check-expect (how-many empty) 0)
;; Exercise 31.3.5
(define (add-to-pi n)
(define (add-to-pi/acc n acc)
(cond
[(zero? n) acc]
[else (add-to-pi/acc (sub1 n) (add1 acc))]))
(add-to-pi/acc n pi))
(check-within (add-to-pi 3) (+ pi 3) 0.00001)
(check-within (add-to-pi 0) (+ pi 0) 0.00001)
(define (add n x)
(define (add/acc n acc)
(cond
[(zero? n) acc]
[else (add/acc (sub1 n) (add1 acc))]))
(add/acc n x))
(check-within (add 3 10.0) 13 0.0001)
;; Exercise 31.3.6
(define (make-palindrome xs)
(define (make-palindrome/acc xs acc)
(cond
[(= 1 (length xs)) acc]
[else (make-palindrome/acc (rest xs)
(cons (first xs) acc))]))
(append xs (make-palindrome/acc xs empty)))
(check-expect (make-palindrome '(a b c)) '(a b c b a))
(check-expect (make-palindrome '(a b c d e)) '(a b c d e d c b a))
;; Exercise 31.3.7
(define (to10 lod)
(define (to10/acc lod acc)
(cond
[(empty? lod) acc]
[else (to10/acc (rest lod) (+ (* 10 acc) (first lod)))]))
(to10/acc lod 0))
(check-expect (to10 (list 1 0 2)) 102)
(check-expect (to10 empty) 0)
(define (to10-general base lod)
(define (to10-general/acc lod acc)
(cond
[(empty? lod) acc]
[else (to10-general/acc (rest lod) (+ (* base acc) (first lod)))]))
(to10-general/acc lod 0))
(check-expect (to10-general 10 (list 1 0 2)) 102)
(check-expect (to10-general 8 (list 1 0 2)) 66)
;; Exercise 31.3.8
(define (is-prime? n)
(define (is-prime/acc? i acc)
(cond
[acc false]
[(= i 1) true]
[else (is-prime/acc? (sub1 i) (zero? (remainder n i)))]))
(is-prime/acc? (sub1 n) true))
(check-expect (is-prime? 3) false)
(test)