-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsicp-exercises.scm
409 lines (337 loc) · 10.3 KB
/
sicp-exercises.scm
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (square x)
(* x x))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(define (sqrt-iter-2 guess old-guess x)
(if (< (abs (- guess old-guess)) .0001)
guess
(sqrt-iter-2 (improve guess x) guess x)))
(define (sqrt-3 x)
(sqrt-iter-2 1.0 100.0 x))
(define (sqrt x)
(sqrt-iter 1.0 x))
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
(define (sqrt-iter2 guess x)
(new-if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(define (sqrt2 x)
(sqrt-iter2 1.0 x))
; exercise 1.23
; for 1.24 need a working runtime function
; exercise 1.26
; exercise 1.27
; exercise 1.28
; non-trivial expmod
(define (expmod-nt base exp m)
(cond ((= exp 0) 1)
((even? exp)
(if (and (not (= base 1))
(not (= base (- m 1)))
(= (remainder (square base) m) 1))
0
(remainder (square (expmod base (/ exp 2) m)) m)))
(else (remainder (* base (expmod base (- exp 1) m)) m))))
; doesn't seem to be working 561 should return #f
(define (miller-rabin-test n)
(define (try-it a)
(if (= (expmod-nt a n n) 0)
#f
(= (expmod-nt a n n) a)))
(try-it (+ 1 (random (- n 1)))))
; from the text:
(define (fermat-test n)
(define (try-it a)
(= (expmod a n n) a))
(try-it (+ 1 (random (- n 1)))))
; next section notes:
; in general lambda is used to create procedures in the same way as define, except that no name is specified for the procedure.
(lambda (<formal parameters>) <body>)
;like:
(lambda (x) (+ x 1))
(define (plus4 x) (+ x 4)) == (define plus4 (lambda (x) (+ 4 x)))
(define (search f neg-point pos-point)
(let ((midpoint (average neg-point pos-point)))
(if (close-enough? neg-point pos-point)
midpoint
(let ((test-value (f midpoint)))
(cond ((positive? test-value)
(search f neg-point midpoint))
((negative? test-value)
(search f midpoint pos-point))
(else midpoint))))))
(define (close-enough? x y) (< (abs (- x y)) 0.001))
(define (half-interval-method f a b)
(let ((a-value (f a))
(b-value (f b)))
(cond ((and (negative? a-value) (positive? b-value))
(search f a b))
((and (negative? b-value) (positive? a-value))
(search f b a))
(else
(error "Values are not of opposite sign" a b)))))
(half-interval-method sin 2.0 4.0)
(define (average x y) (/ (+ x y) 2))
(define tolerance 0.00001)
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (try guess)
(let ((next (f guess)))
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
(define (sqrt x)
(fixed-point (lambda (y) (average y (/ x y))) 1.0))
; ^^ this technique is called average damping
; from other file:
(define (cube a) (* a a a))
(define (sum-cubes a b)
(if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))
(sum-cubes 1 2)
(+ (cube 1) (sum-cubes (+ 1 1) 2))
(+ 1 (sum-cubes 2 2))
(+ 1 (+ (cube 2) (sum-cubes (+ 2 1) 2)))
(+ 1 (+ 8 0))
9
(define (inc n) (+ n 1))
(define (sum-cubes a b)
(sum cube a inc b))
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
(define (identity x) x)
(define (sum-ints a b)
(sum identity a inc b))
(define (integral f a b dx)
(define (add-dx x) (+ x dx))
(* (sum f (+ a (/ dx 2.0)) add-dx b) dx))
(integral cube 0 1 0.01)
(* (sum cube (+ 0 (/ 0.01 2.0)) add-dx 1) 0.01)
(* (+ (cube 0) (sum cube (add-dx 0) add-dx 1)) 0.01)
(* (+ (cube 0) (sum cube (+ 0 0.01) add-dx 1)) 0.01)
(* (+ (cube 0) (sum cube 0.01 add-dx 1)) 0.01)
(* (+ (cube 0) (+ (cube 0.01) (sum cube (add-dx 0.01) add-dx 1))) 0.01)
(* (+ 0 (+ 1.0e-6 (sum cube (add-dx 0.01) add-dx 1))) 0.01)
(* (+ 0 (+ 1.0e-6 (sum cube 0.02 add-dx 1))) 0.01)
(* (+ 0 (+ 1.0e-6 (+ 8.0e-6 (sum cube (add-dx 0.02) add-dx 1)))) 0.01)
(integral cube 0 1 0.01)
.24998750000000042
(integral cube 0 1 0.001)
.249999875000001
(The exact value of the integral of cube between 0 and 1 is 1/4.)
h = (b - a) / n ; for some even integer n
ysub k = f (a + kh)
(define (simpson f a b n)
(let (h (/ (- b a) n))
(define (next-a k)
(* (+ 1 (remainder k 2)) (+ a (* k h))))
(* (/ h 3) (sum f a next-a b))))
; again!
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
(define (simpsons f a b n)
(define h (/ (- b a) n))
(define (inc x) (+ x 1))
(define (y k)
(f (+ a (* k h))))
(define (term k)
(* (cond ((odd? k) 4)
((or (= k 0) (= k n)) 1)
((even? k) 2))
(y k)))
(/ (* h (sum term 0 inc n)) 3))
(define (cube x) (* x x x))
(simpsons cube 0 1 4)
(/ (* (/ (- 1 0) 4) (sum term 0 inc 3)) 3)
(/ (* .25 (sum term 0 inc 4)) 3)
(/ (* .25 (+ (term 0) (sum term (inc 0) inc 4))) 3)
(/ (* .25 (+ (* 1 (y 0)) (sum term (inc 0) inc 4))) 3)
(/ (* .25 (+ (* 1 (f (+ 0 (* 0 .25))))) (sum term (inc 0) inc 4))) 3)
(define (reverse l)
(if (null? (cdr l))
l
(append (reverse (cdr l)) (list (car l)))))
; accumulate form:
(define (reverse l)
(define (helper li ac)
(if (null? (cdr li))
(cons (car li) ac)
(helper (cdr li) (cons (car li) ac))))
(helper (cdr l) (cons (car l) ())))
; TODO this returns incorrectly for (list 1 2 3 4)
(define (deep-reverse l)
(if (not (list? l))
l
(if (null? (cdr l))
(deep-reverse (car l))
;; probably just need to use append instead of cons here
(cons (deep-reverse (cdr l)) (list (deep-reverse (car l)))))))
(define x (list (list 1 2) (list 3 4)))
(reverse x)
; ((3 4) (1 2))
(deep-reverse x)
; ((4 3) (2 1))
; exercise 2.30
(define (square-tree tree)
(cond ((null? tree) ())
((not (pair? tree)) (* tree tree))
(else (cons (square-tree (car tree))
(square-tree (cdr tree))))))
(define (square-tree tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(square-tree sub-tree)
(* sub-tree sub-tree)))
tree))
(square-tree
(list 1
(list 2 (list 3 4) 5)
(list 6 7)))
; exercise 2.31
(define (tree-map fn tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(square-tree sub-tree)
(fn sub-tree)))
tree))
(define (square-tree tree) (tree-map square tree))
; exercise 2.32
(define (subsets s)
(if (null? s)
(list ())
(let ((rest (subsets (cdr s))))
(append rest (map (lambda (x)
(cons (car s) x)) rest)))))
(subsets (list 1 2 3))
(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))
rest will be a list of lists, so when you cons the car of the current s
with each of rest's members the result is a list of lists with the first element
of the passed list (s) added to it. The reason you don't end up with any lists containing
a distinct element and nil (for example (2 ()) ) is due to the base case returning a
list containing nil, which gets decomposed by map (which returns a list) and
then getting appended to the recursive result of the (subsets) of the cdr of the current set.
Or explained via the example given:
in the example given the recursive subset calls result in ((2 3) (2) (3) ()) so when cons'ing with
1 we get ((1 2 3) (1 2) (1 3) (1)) - which gets appended to the result
this repeats for (2 3) where we get ((3) ())
when cons'ed with 2 we get ((2 3) (2)) - which gets appended to the result
this repeats for (3) where we get ()
when cons'ed with 3 we get ((3) ()) - which gets appended to the result
; exercise 2.34
(define (horner-eval x coefficient-sequence)
(accumulate (lambda (this-coeff higher-terms)
(if (pair? higher-terms)
(+ this-coeff (* x (horner-eval x higher-terms)))
(+ this-coeff (* x higher-terms))))
0
coefficient-sequence))
(horner-eval 2 (list 1 3 0 5 0 1)); 79
; exercise 2.35
; from the text:
(define (count-leaves x)
(cond ((null? x) 0)
((not (pair? x)) 1)
(else (+ (count-leaves (car x))
(count-leaves (cdr x))))))
; from the text:
(define (enumerate-tree tree)
(cond ((null? tree) ())
((not (pair? tree)) (list tree))
(else (append (enumerate-tree (car tree))
(enumerate-tree (cdr tree))))))
(enumerate-tree (list 1 (list 2 (list 3 4)) 5))
; (1 2 3 4 5)
(define (count-leaves t)
(accumulate + 0 (map (lambda (r) 1) (enumerate-tree t))))
(define y (list 1 (list 2 (list 3 4)) 5))
(define x (list (list 1 2) (list 3 4)))
(length x); 3
(count-leaves x); 4
(count-leaves y); 5
; exercise 2.36
(define (accumulate-n op init seqs)
(if (null? (car seqs))
()
(cons (accumulate op init (map (lambda (x) (car x)) seqs))
(accumulate-n op init (map (lambda (x) (cdr x)) seqs)))))
(define y (list (list 1 2 3) (list 4 5 6) (list 7 8 9) (list 10 11 12)))
(accumulate-n + 0 y)
; exercise 2.37
(define mat (list (list 1 2 3 4) (list 4 5 6 6) (list 6 7 8 9)))
(define vec (list 2 2 2 2))
(define (dot-product v w)
(accumulate + 0 (map * v w)))
(define (matrix-*-vector m v)
(map (lambda (x) (accumulate-n * 1 (list x v))) m))
(matrix-*-vector mat vec)
;Value 35: ((2 4 6 8) (8 10 12 12) (12 14 16 18))
(define (transpose mat)
(accumulate-n cons () mat))
(transpose mat)
; ((1 4 6) (2 5 7) (3 6 8) (4 6 9))
; mat:
((1 2 3 4)
(4 5 6 6)
(6 7 8 9))
; transpose of mat:
((1 4 6)
(2 5 7)
(3 6 8)
(4 6 9))
(define (matrix-*-matrix m n)
(let ((cols (transpose n)))
(map (lambda (y)
(map (lambda (x)
(accumulate +
0
(accumulate-n *
1
(list y x))))
mat))
mat)))
(define b (transpose mat))
(matrix-*-matrix mat b)
;((30 56 80) (56 113 161) (80 161 230))
; exercise 2.38
(define (fold-left op initial sequence)
(define (iter result rest)
(if (null? rest)
result
(iter (op result (car rest))
(cdr rest))))
(iter initial sequence))
(define fold-right accumulate)
(fold-right / 1 (list 1 2 3)); 3/2
(fold-left / 1 (list 1 2 3)); 1/6
(fold-right list () (list 1 2 3)); (1 (2 (3 ())))
(fold-left list () (list 1 2 3)); (((() 1) 2) 3)
The operation must produce the same value regardless of the
input order. For example addition and multiplication produce the same output:
(fold-right * 1 (list 1 2 3)); 6
(fold-left * 1 (list 1 2 3)); 6
(fold-right + 1 (list 1 2 3)); 7
(fold-left + 1 (list 1 2 3)); 7
; exercise 2.39
(define (reverse sequence)
(fold-right (lambda (x y) (append y (list x))) () sequence))
(reverse (list 1 2 3))
(define (reverse sequence)
(fold-left (lambda (x y) (cons y x)) () sequence))
(reverse (list 1 2 3))