-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch1.scm
2046 lines (1687 loc) · 58 KB
/
ch1.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;;CODE FROM CHAPTER 1 OF STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS
;;; Examples from the book are commented out with ;: so that they
;;; are easy to find and so that they will be omitted if you evaluate a
;;; chunk of the file (programs with intervening examples) in Scheme.
;;; BEWARE: Although the whole file can be loaded into Scheme,
;;; don't expect the programs to work if you do so. For example,
;;; the redefinition of + in exercise 1.9 wreaks havoc with the
;;; last version of square defined here.
;;;SECTION 1.1.1
;; interpreter examples
;: 486
;: (+ 137 349)
;: (- 1000 334)
;: (* 5 99)
;: (/ 10 5)
;: (+ 2.7 10)
;: (+ 21 35 12 7)
;: (* 25 4 12)
;: (+ (* 3 5) (- 10 6))
;: (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))
;: (+ (* 3
;: (+ (* 2 4)
;: (+ 3 5)))
;: (+ (- 10 7)
;: 6))
;;;SECTION 1.1.2
;: (define size 2)
;: size
;: (* 5 size)
;: (define pi 3.14159)
;: (define radius 10)
;: (* pi (* radius radius))
;: (define circumference (* 2 pi radius))
;: circumference
;;;SECTION 1.1.3
;: (* (+ 2 (* 4 6))
;: (+ 3 5 7))
;;;SECTION 1.1.4
(define (square x) (* x x))
;: (square 21)
;: (square (+ 2 5))
;: (square (square 3))
(define (sum-of-squares x y)
(+ (square x) (square y)))
;: (sum-of-squares 3 4)
(define (f a)
(sum-of-squares (+ a 1) (* a 2)))
;: (f 5)
;;;SECTION 1.1.5
;: (f 5)
;: (sum-of-squares (+ 5 1) (* 5 2))
;: (+ (square 6) (square 10))
;: (+ (* 6 6) (* 10 10))
;: (+ 36 100)
;: (f 5)
;: (sum-of-squares (+ 5 1) (* 5 2))
;: (+ (square (+ 5 1)) (square (* 5 2)) )
;: (+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2)))
;: (+ (* 6 6) (* 10 10))
;: (+ 36 100)
;: 136
;;;SECTION 1.1.6
(define (abs x)
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (- x))))
(define (abs x)
(cond ((< x 0) (- x))
(else x)))
(define (abs x)
(if (< x 0)
(- x)
x))
;: (and (> x 5) (< x 10))
(define (>= x y)
(or (> x y) (= x y)))
(define (>= x y)
(not (< x y)))
;;EXERCISE 1.1
;: 10 ;10
;: (+ 5 3 4) ;12
;: (- 9 1) ;8
;: (/ 6 2) ;4
;: (+ (* 2 4) (- 4 6)) ;6
;: (define a 3) ;a
;: (define b (+ a 1)) ;b
;: (+ a b (* a b)) ;19
;: (= a b) ;#f
;: (if (and (> b a) (< b (* a b)))
;: b
;: a)
;4
;: (cond ((= a 4) 6)
;: ((= b 4) (+ 6 7 a))
;: (else 25))
;16
;: (+ 2 (if (> b a) b a)) ;6
;: (* (cond ((> a b) a)
;: ((< a b) b)
;: (else -1))
;: (+ a 1))
;16
;;EXERCISE 1.2
(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
(* 3 (- 6 2) (- 2 7)))
;;EXERCISE 1.3
(define (f x y z)
(define (sum-squares a b) (+ (square a) (square b)))
(if (>= x y)
(if (>= y z)
(sum-squares x y)
(sum-squares x z))
(sum-squares y z)))
;;EXERCISE 1.4
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
; The body of the procedure consists of a single combination whose operator
; evaluates to + or - depending whether b is positive or nonpositive. The
; procedure is thus equivalent to (+ a b) for positive values of b and (- a b)
; for nonpositive values of b. Since adding a positive number or subtracting a
; nonpositive number are both equivalent to adding the number's absolute value,
; the body of the procedure is also equivalent to (+ a (abs b)).
;;EXERCISE 1.5
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y))
;: (test 0 (p))
; With applicative-order evaluation: Evaluating the expression (test 0 (p))
; requires first to evaluate the operator and operands. The second operand (p)
; is a combination whose operator evaluates to itself due to the recursive
; definition (define (p) (p)). So evaluating the second operand never
; terminates, and evaluating (test 0 (p)) will cause the interpreter to hang
; indefinitely.
;
; With normal-order evaluation: The expression (test 0 (p)) expands to
; (if (= 0 0) 0 (p)) under evaluation. Since the predicate is true, the
; expression evaluates to the consequent branch automatically, and the
; alternative branch (p) need never be evaluated. Evaluating this expression
; yields 0.
;;;SECTION 1.1.7
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
(define (improve guess x)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (sqrt x)
(sqrt-iter 1.0 x))
;: (sqrt 9)
;: (sqrt (+ 100 37))
;: (sqrt (+ (sqrt 2) (sqrt 3)))
;: (square (sqrt 1000))
;;EXERCISE 1.6
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
;: (new-if (= 2 3) 0 5)
;: (new-if (= 1 1) 0 5)
(define (sqrt-iter guess x)
(new-if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
; Using the new sqrt-iter procedure results in infinite recursion.
;
; Since new-if is an ordinary procedure, not a special form, it cannot be
; evaluated without first evaluating all its operands. Inside the new sqrt-iter,
; the alternative branch (third operand) to new-if consists of a recursive call
; to sqrt-iter which, for the reason just stated, will always be evaluated
; regardless of the predicate (first operand). Thus the evaluation of sqrt-iter
; results in an infinitely nested series of calls to sqrt-iter and new-if. Since
; none of these calls ever returns, we would expect evaluation to continue
; indefinitely until a stack overflow occurs. (In fact, in MIT/GNU Scheme
; running with --stack 1000, the error is "maximum recursion depth exceeded".)
;;EXERCISE 1.7
; We can use error as a percentage of the expected (correct) value as a measure
; of the effectiveness of our square root procedure. For example, for
; (sqrt 100), the expected value is 10. If our procedure returns 11, then the
; error is 10%.
;
; For very small radicands, the acceptable absolute error 0.001 will tend to be
; very high as a percentage of the expected value of the root, and so the
; returned values will tend to miss the mark by a high percentage as well. For
; example, for (sqrt 0.000009) the exact correct root is 0.003 but the
; calculated root is 0.03134584760656851, an error of more than 900%.
;
; For very large radicands, with a limited-precision floating-point
; representation, we can end up with a guess that is impossible to improve -- it
; may even be precisely the correct answer! -- yet never passes the good-enough?
; test, leading to infinite recursion.
;
; As an example, consider (sqrt 1e100). The expected value of the root is 1e50.
; Let's say that during evaluation of (sqrt 1e100), we arrive at 1e50 as a
; guess. We'd ideally like to stop here and return 1e50 as the answer. But
; observe that, due to lost precision, evaluating (square 1e50) in the REPL
; yields not 1e100 but 1.0000000000000002e100, which differs from the supplied
; radicand 1e100 by more than the cutoff value of 0.001. Thus, 1e50 doesn't pass
; the good-enough? test, and as the correct answer doesn't pass, we can see that
; any other answer we might arrive at will not pass either, and so our
; evaluation will continue to "improve" and test guesses indefinitely.
(define (sqrt-iter guess old-guess x)
(if (good-enough? guess old-guess x)
guess
(sqrt-iter (improve guess x)
guess
x)))
(define (good-enough? guess old-guess x)
(< (abs (/ (- guess old-guess) guess)) 1.0e-10))
(define (sqrt x)
(sqrt-iter 1.0 0.0 x))
; The revised square-root procedure above works better for both small and large
; numbers. It is more accurate overall, and for very large numbers, it returns a
; value in cases where the original procedure would have run indefinitely.
;;EXERCISE 1.8
(define (cbrt-iter guess old-guess x)
(if (good-enough? guess old-guess x)
guess
(cbrt-iter (cbrt-improve guess x)
guess
x)))
(define (cbrt-improve guess x)
(/ (+ (/ x (square guess)) (* 2 guess)) 3))
(define (cbrt x)
(cbrt-iter 1.0 0.0 x))
;;;SECTION 1.1.8
(define (square x) (* x x))
(define (square x)
(exp (double (log x))))
(define (double x) (+ x x))
;; As in 1.1.7
(define (sqrt x)
(sqrt-iter 1.0 x))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess x)
(average guess (/ x guess)))
;; Block-structured
(define (sqrt x)
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess x)
(average guess (/ x guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(sqrt-iter 1.0 x))
;; Taking advantage of lexical scoping
(define (sqrt x)
(define (good-enough? guess)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess)
(average guess (/ x guess)))
(define (sqrt-iter guess)
(if (good-enough? guess)
guess
(sqrt-iter (improve guess))))
(sqrt-iter 1.0))
;;;SECTION 1.2.1
;; Recursive
(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1)))))
;; Iterative
(define (factorial n)
(fact-iter 1 1 n))
(define (fact-iter product counter max-count)
(if (> counter max-count)
product
(fact-iter (* counter product)
(+ counter 1)
max-count)))
;; Iterative, block-structured (from footnote)
(define (factorial n)
(define (iter product counter)
(if (> counter n)
product
(iter (* counter product)
(+ counter 1))))
(iter 1 1))
;;EXERCISE 1.9
(define (+ a b)
(if (= a 0)
b
(inc (+ (dec a) b))))
; The first procedure generates a recursive process.
;
; (+ 4 5)
; (if (= 0 4) 5 (inc (+ (dec 4) 5)))
; (inc (+ (dec 4) 5))
; (inc (+ 3 5))
; (inc (if (= 0 3) 5 (inc (+ (dec 3) 5))))
; (inc (inc (+ (dec 3) 5)))
; (inc (inc (+ 2 5)))
; (inc (inc (if (= 0 2) 5 (inc (+ (dec 2) 5)))))
; (inc (inc (inc (+ (dec 2) 5))))
; (inc (inc (inc (+ 1 5))))
; (inc (inc (inc (if (= 0 1) 5 (inc (+ (dec 1) 5))))))
; (inc (inc (inc (inc (+ (dec 1) 5)))))
; (inc (inc (inc (inc (+ 0 5)))))
; (inc (inc (inc (inc (if (= 0 5) 5 (inc (+ (dec 0) 5)))))))
; (inc (inc (inc (inc 5))))
; (inc (inc (inc 6)))
; (inc (inc 7))
; (inc 8)
; 9
(define (+ a b)
(if (= a 0)
b
(+ (dec a) (inc b))))
; The second procedure generates an iterative process.
;
; (+ 4 5)
; (if (= 4 0) 5 (+ (dec 4) (inc 5)))
; (+ (dec 4) (inc 5))
; (+ 3 6)
; (if (= 3 0) 6 (+ (dec 3) (inc 6)))
; (+ (dec 3) (inc 6))
; (+ 2 7)
; (if (= 2 0) 7 (+ (dec 2) (inc 7)))
; (+ (dec 2) (inc 7))
; (+ 1 8)
; (if (= 1 0) 8 (+ (dec 1) (inc 8)))
; (+ (dec 1) (inc 8))
; (+ 0 9)
; (if (= 0 0) 9 (+ (dec 0) (inc 9)))
; 9
;;EXERCISE 1.10
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))
;: (A 1 10) ;1024
;: (A 2 4) ;65536
;: (A 3 3) ;65536
(define (f n) (A 0 n)) ;(f n) computes 2*n.
(define (g n) (A 1 n)) ;(g n) computes 2^n.
(define (h n) (A 2 n)) ;(h n) computes 2↑↑n.
; The last answer uses Knuth up-arrow notation. It is equivalent to saying that
; (h n) computes P(n) where
;
; P(n) = / 2 if n = 1
; \ 2^P(n-1) otherwise.
(define (k n) (* 5 n n))
;;;SECTION 1.2.2
;; Recursive
(define (fib n)
(cond ((= n 0) 0)
((= n 1) 1)
(else (+ (fib (- n 1))
(fib (- n 2))))))
;; Iterative
(define (fib n)
(fib-iter 1 0 n))
(define (fib-iter a b count)
(if (= count 0)
b
(fib-iter (+ a b) a (- count 1))))
;; Counting change
(define (count-change amount)
(cc amount 5))
(define (cc amount kinds-of-coins)
(cond ((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)
(else (+ (cc amount
(- kinds-of-coins 1))
(cc (- amount
(first-denomination kinds-of-coins))
kinds-of-coins)))))
(define (first-denomination kinds-of-coins)
(cond ((= kinds-of-coins 1) 1)
((= kinds-of-coins 2) 5)
((= kinds-of-coins 3) 10)
((= kinds-of-coins 4) 25)
((= kinds-of-coins 5) 50)))
;: (count-change 100)
;;EXTRA CREDIT
; Here is a more efficient version without using memoization. It was inspired by
; the observation most kinds of coin are exact multiples of each other: one
; half-dollar equals two quarters, one quarter equals five nickels, and one
; nickel equals five pennies. One can use this information to build an efficient
; recursive algorithm for calculating change using only this limited subset of
; coins, then add in special handling for dimes.
(define (cc kind n-coins n-subcoins)
(define n-unused-subcoins
(- n-subcoins (* n-coins (if (= kind 5) 2 5))))
(if (or (> kind 5) (< n-unused-subcoins 0))
0
(+ 1 (cc kind (+ n-coins 1) n-subcoins)
(cc (+ kind (if (= kind 2) 2 1)) 1 n-coins)
(cond ((= kind 2) (quotient n-coins 2))
((and (= kind 4) (> n-coins 0))
(* (quotient n-unused-subcoins 2)
(+ 1 (quotient n-coins 2))))
(else 0)))))
(define (count-change amount)
(if (< amount 0) 0 (+ 1 (cc 2 1 amount))))
;;EXERCISE 1.11
(define (f-recursive n)
(define (addend m)
(* m (f-recursive (- n m))))
(if (< n 3) n (+ (addend 1) (addend 2) (addend 3))))
(define (f-iterative n)
(define (solve a b c)
(+ (* 3 a) (* 2 b) c))
(define (go a b c count)
(if (= count 0) a (go b c (solve a b c) (- count 1))))
(go 0 1 2 n))
;;EXERCISE 1.12
(define (pascal n)
(define (f row col)
(cond ((or (= col 0) (= col row)) 1)
((or (< col 0) (> col row)) 0)
(else (+ (f (- row 1) (- col 1))
(f (- row 1) col)))))
(define (g row col count)
(cond ((> col row) (g (+ row 1) 0 count))
((= count 0) (f row col))
(else (g row (+ col 1) (- count 1)))))
(g 0 0 n))
;;EXERCISE 1.13
; Let
;
; ϕ = (1 + √5)/2 and
; ψ = (1 - √5)/2.
;
; Note that ϕ and ψ are positive and negative respectively, and that ψ² < 1 (the
; proofs are omitted for brevity).
;
; We will first prove that the following statement P(n) holds for all natural
; numbers n.
;
; Fib(n) = (ϕⁿ - ψⁿ)/√5
;
; It is easy to show that P(0) holds:
;
; Fib(0) = (((1 + √5)/2)⁰ - ((1 - √5)/2)⁰)/√5 by substitution
; Fib(0) = 0
; 0 = 0 by the definition of Fib(n)
;
; And similarly P(1):
;
; Fib(1) = (((1 + √5)/2)¹ - ((1 - √5)/2)¹)/√5 by substitution
; Fib(1) = 1
; 1 = 1 by the definition of Fib(n)
;
; Now we will show that for some i, if P(i) holds and P(i-1) holds, then P(i+1)
; also holds.
;
; Fib(i+1) = (ϕⁱ⁺¹ - ψⁱ⁺¹)/√5 P(i+1)
; Fib(i) + Fib(i-1) = (ϕⁱ⁺¹ - ψⁱ⁺¹)/√5 by definition of Fib(n)
; (ϕⁱ - ψⁱ)/√5 + (ϕⁱ⁻¹ - ψⁱ⁻¹)/√5 = (ϕⁱ⁺¹ - ψⁱ⁺¹)/√5 because P(i) and P(i-1)
; ϕⁱ - ψⁱ + ϕⁱ⁻¹ - ψⁱ⁻¹ = ϕⁱ⁺¹ - ψⁱ⁺¹
; ϕⁱ⁻¹ + ϕⁱ - ϕⁱ⁺¹ = ψⁱ⁻¹ + ψⁱ - ψⁱ⁺¹
; ϕⁱ(ϕ⁻¹ + 1 - ϕ) = ψⁱ(ψ⁻¹ + 1 - ψ) (reduction omitted for
; ϕⁱ(0) = ψⁱ(0) brevity)
; 0 = 0
;
; To prove that Fib(n) is the closest integer to ϕⁿ/√5, we will show that
; |Fib(n) - ϕⁿ/√5| < 1/2 holds for all natural numbers n.
;
; |Fib(n) - ϕⁿ/√5| < 1/2
; |(ϕⁿ - ψⁿ)/√5 - ϕⁿ/√5| < 1/2 because P(n)
; |-ψⁿ| < √5/2
; |-ψⁿ|² < (√5/2)²
; (ψ²)ⁿ < 5/4
; 1ⁿ < 5/4 because ψ² < 1 and n ≥ 0
; 1 < 5/4 QED
;;;SECTION 1.2.3
;;EXERCISE 1.14
(define (visualize-count-change amount)
(define (go amount kinds-of-coins prefix tee?)
(define child-prefix
(string-append prefix (if tee? " │ " " ")))
(define (draw-child-prefix tee? value)
(display (string-append child-prefix (if tee? " ├─" " └─") value)))
(display (list "cc" amount kinds-of-coins))
(newline)
(cond ((= amount 0)
(begin (draw-child-prefix false "1\n") 1))
((or (< amount 0) (= kinds-of-coins 0))
(begin (draw-child-prefix false "0\n") 1))
(else
(let ((qty1 (begin (draw-child-prefix true "")
(go amount
(- kinds-of-coins 1)
child-prefix
true))))
(let ((qty2 (begin (draw-child-prefix false "")
(go (- amount
(first-denomination kinds-of-coins))
kinds-of-coins
child-prefix
false))))
(+ qty1 qty2 1))))))
(go amount 5 "" false))
; The procedure above visualizes the process generated by count-change and
; returns the total number of recursive calls to cc. The space used by the
; process grows as Θ(n) and the number of steps as Θ(2ⁿ).
;
; (visualize-count-change 11)
;
; Output:
;
; (cc 11 5)
; ├─(cc 11 4)
; │ ├─(cc 11 3)
; │ │ ├─(cc 11 2)
; │ │ │ ├─(cc 11 1)
; │ │ │ │ ├─(cc 11 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 10 1)
; │ │ │ │ ├─(cc 10 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 9 1)
; │ │ │ │ ├─(cc 9 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 8 1)
; │ │ │ │ ├─(cc 8 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 7 1)
; │ │ │ │ ├─(cc 7 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 6 1)
; │ │ │ │ ├─(cc 6 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 5 1)
; │ │ │ │ ├─(cc 5 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 4 1)
; │ │ │ │ ├─(cc 4 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 3 1)
; │ │ │ │ ├─(cc 3 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 2 1)
; │ │ │ │ ├─(cc 2 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 1 1)
; │ │ │ │ ├─(cc 1 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 0 1)
; │ │ │ │ └─1
; │ │ │ └─(cc 6 2)
; │ │ │ ├─(cc 6 1)
; │ │ │ │ ├─(cc 6 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 5 1)
; │ │ │ │ ├─(cc 5 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 4 1)
; │ │ │ │ ├─(cc 4 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 3 1)
; │ │ │ │ ├─(cc 3 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 2 1)
; │ │ │ │ ├─(cc 2 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 1 1)
; │ │ │ │ ├─(cc 1 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 0 1)
; │ │ │ │ └─1
; │ │ │ └─(cc 1 2)
; │ │ │ ├─(cc 1 1)
; │ │ │ │ ├─(cc 1 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 0 1)
; │ │ │ │ └─1
; │ │ │ └─(cc -4 2)
; │ │ │ └─0
; │ │ └─(cc 1 3)
; │ │ ├─(cc 1 2)
; │ │ │ ├─(cc 1 1)
; │ │ │ │ ├─(cc 1 0)
; │ │ │ │ │ └─0
; │ │ │ │ └─(cc 0 1)
; │ │ │ │ └─1
; │ │ │ └─(cc -4 2)
; │ │ │ └─0
; │ │ └─(cc -9 3)
; │ │ └─0
; │ └─(cc -14 4)
; │ └─0
; └─(cc -39 5)
; └─0
;;EXERCISE 1.15
(define (cube x) (* x x x))
(define (p x) (- (* 3 x) (* 4 (cube x))))
(define (sine angle)
(if (not (> (abs angle) 0.1))
angle
(p (sine (/ angle 3.0)))))
; a. When evaluating (sine 12.15), p is applied five times.
; b. For (sine a), the order of growth in both space and # of steps is Θ(log a).
;;;SECTION 1.2.4
;; Linear recursion
(define (expt b n)
(if (= n 0)
1
(* b (expt b (- n 1)))))
;; Linear iteration
(define (expt b n)
(expt-iter b n 1))
(define (expt-iter b counter product)
(if (= counter 0)
product
(expt-iter b
(- counter 1)
(* b product))))
;; Logarithmic iteration
(define (fast-expt b n)
(cond ((= n 0) 1)
((even? n) (square (fast-expt b (/ n 2))))
(else (* b (fast-expt b (- n 1))))))
(define (even? n)
(= (remainder n 2) 0))
;;EXERCISE 1.16
(define (iterative-fast-expt b n)
(define (go a b n)
(cond ((= n 0) a)
((even? n) (go a (square b) (/ n 2)))
(else (go (* a b) b (- n 1)))))
(go 1 b n))
;;EXERCISE 1.17
(define (* a b)
(if (= b 0)
0
(+ a (* a (- b 1)))))
(define (double x) (+ x x))
(define (halve x) (/ x 2))
(define (*-recursive x y)
(cond ((= y 0) 0)
((= y 1) x)
((even? y) (*-recursive (double x) (halve y)))
(else (+ x (*-recursive x (- y 1))))))
;;EXERCISE 1.18
(define (*-iterative x y)
(define (go acc x y)
(cond ((= y 0) acc)
((even? y) (go acc (double x) (halve y)))
(else (go (+ acc x) x (- y 1)))))
(go 0 x y))
;;EXERCISE 1.19
; Given
;
; Tpq(a,b) = (bq + aq + ap, bp + aq)
;
; we will show that Tpq(Tpq(a,b)) is equivalent to Tp'q'(a,b) where
;
; p' = p² + q² and
; q' = q² + 2pq.
;
; Tpq(Tpq(a,b)) = Tpq(bq + aq + ap, bp + aq)
; = ((bp + aq)q + (bq + aq + ap)q + (bq + aq + ap)p,
; (bp + aq)p + (bq + aq + ap)q)
; = (bpq + aq² + bq² + aq² + apq + bpq + apq + ap²,
; bp² + apq + bq² + aq² + apq)
; = ((bq² + 2bpq) + (aq² + 2apq) + (ap² + aq²),
; (bp² + bq²) + (aq² + 2apq))
; = (b(q² + 2pq) + a(q² + 2pq) + a(p² + q²),
; b(p² + q²) + a(q² + 2pq))
; = (bq' + aq' + ap', bp' + aq')
; = Tp'q'(a,b) QED.
(define (fib n)
(fib-iter 1 0 0 1 n))
(define (fib-iter a b p q count)
(cond ((= count 0) b)
((even? count)
(fib-iter a
b
(+ (square p) (square q)) ; compute p'
(+ (square q) (* 2 p q)) ; compute q'
(/ count 2)))
(else (fib-iter (+ (* b q) (* a q) (* a p))
(+ (* b p) (* a q))
p
q
(- count 1)))))
;;;SECTION 1.2.5
(define (gcd a b)
(if (= b 0)
a
(gcd b (remainder a b))))
;;EXERCISE 1.20
; (visualize-normal-order expr) generates a visualization of the normal-order
; evaluation of expr. The remainder procedure is abbreviated as r for brevity.
; Each step during which a remainder operation was actually performed is
; prefixed with an annotation, and the return value is the total number of
; remainder operations performed.
(define (visualize-normal-order expr)
; (eval e) returns a pair (e2 . r) where e2 is e evaluated one additional step
; and r is true iff a remainder operation was performed.
(define (eval expr)
(cond ((not (equal? (subst expr) expr)) (cons (subst expr) false))
((or (boolean? expr) (number? expr)) (cons expr false))
((eq? (car expr) 'if) (eval-if expr))
(else (eval-binop expr))))
(define (eval-if expr)
(let ((arg1 (cadr expr))
(arg2 (caddr expr))
(arg3 (cadddr expr)))
(let ((pair1 (eval arg1)))
(if (equal? (car pair1) arg1)
(cons (if arg1 arg2 arg3) false)
(cons (list 'if (car pair1) arg2 arg3) (cdr pair1))))))
(define (eval-binop expr)
(let ((op (car expr))
(arg1 (cadr expr))
(arg2 (caddr expr)))
(let ((pair1 (eval arg1))
(pair2 (eval arg2)))
(cond ((not (equal? (car pair1) arg1))
(cons (list op (car pair1) arg2) (cdr pair1)))
((not (equal? (car pair2) arg2))
(cons (list op arg1 (car pair2)) (cdr pair2)))
((eq? op '=) (cons (= arg1 arg2) false))
((eq? op 'r) (cons (remainder arg1 arg2)
(list 'remainder arg1 arg2)))
(else (error "unknown operand" op))))))
(define (subst expr)
(cond ((or (number? expr) (boolean? expr)) expr)
((eq? (car expr) 'gcd)
(let ((a (cadr expr))
(b (caddr expr)))
(list 'if (list '= b 0) a (list 'gcd b (list 'r a b)))))
(else expr)))
(define (show pair)
(if (cdr pair)
(begin (display "; operation performed: ")
(display (cdr pair))
(newline)))
(pp (car pair) (current-output-port) true)
(newline))
(define (go pair count)
(show pair)
(let ((expr (car pair))
(incr (if (cdr pair) 1 0)))
(let ((pair2 (eval expr)))
(if (not (equal? expr (car pair2)))
(go (eval expr) (+ count incr))
count))))
(go (cons expr false) 0))
; (count-remainder-ops a b) calculates the number of remainder operations that
; would be performed during applicative-order evaluation of (gcd a b).
(define (count-remainder-ops a b)
(define (go a b count)
(if (= b 0)
count
(go b (remainder a b) (+ count 1))))
(go a b 0))
; The remainder operation is performed 17 times in normal-order evaluation, and
; only 4 times in applicative-order evaluation, as shown below.
;
; (count-remainder-ops 206 40) ; 4
;
; (visualize-normal-order '(gcd 206 40)) ; 17
;
; Output:
;
; (gcd 206 40)
;
; (if (= 40 0)
; 206
; (gcd 40 (r 206 40)))
;
; (if #f
; 206
; (gcd 40 (r 206 40)))
;
; (gcd 40 (r 206 40))
;
; (if (= (r 206 40) 0)
; 40
; (gcd (r 206 40) (r 40 (r 206 40))))
;
; ; operation performed: (remainder 206 40)
; (if (= 6 0)
; 40
; (gcd (r 206 40) (r 40 (r 206 40))))
;
; (if #f
; 40
; (gcd (r 206 40) (r 40 (r 206 40))))
;
; (gcd (r 206 40) (r 40 (r 206 40)))
;
; (if (= (r 40 (r 206 40)) 0)
; (r 206 40)
; (gcd (r 40 (r 206 40)) (r (r 206 40) (r 40 (r 206 40)))))
;
; ; operation performed: (remainder 206 40)
; (if (= (r 40 6) 0)
; (r 206 40)
; (gcd (r 40 (r 206 40)) (r (r 206 40) (r 40 (r 206 40)))))
;
; ; operation performed: (remainder 40 6)
; (if (= 4 0)
; (r 206 40)
; (gcd (r 40 (r 206 40)) (r (r 206 40) (r 40 (r 206 40)))))
;
; (if #f
; (r 206 40)
; (gcd (r 40 (r 206 40)) (r (r 206 40) (r 40 (r 206 40)))))
;
; (gcd (r 40 (r 206 40)) (r (r 206 40) (r 40 (r 206 40))))
;
; (if (= (r (r 206 40) (r 40 (r 206 40))) 0)
; (r 40 (r 206 40))
; (gcd (r (r 206 40) (r 40 (r 206 40)))
; (r (r 40 (r 206 40)) (r (r 206 40) (r 40 (r 206 40))))))
;
; ; operation performed: (remainder 206 40)
; (if (= (r 6 (r 40 (r 206 40))) 0)
; (r 40 (r 206 40))
; (gcd (r (r 206 40) (r 40 (r 206 40)))