Blame


1 665c255d 2023-08-04 jrmu ;; Exercise 1.32. a. Show that sum and product (exercise 1.31) are both special cases of a still more general notion called accumulate that combines a collection of terms, using some general accumulation function:
2 665c255d 2023-08-04 jrmu
3 665c255d 2023-08-04 jrmu ;; (accumulate combiner null-value term a next b)
4 665c255d 2023-08-04 jrmu
5 665c255d 2023-08-04 jrmu ;; Accumulate takes as arguments the same term and range specifications as sum and product, together with a combiner procedure (of two arguments) that specifies how the current term is to be combined with the accumulation of the preceding terms and a null-value that specifies what base value to use when the terms run out. Write accumulate and show how sum and product can both be defined as simple calls to accumulate.
6 665c255d 2023-08-04 jrmu
7 665c255d 2023-08-04 jrmu (define (accumulate combiner null-value term a next b)
8 665c255d 2023-08-04 jrmu (if (> a b)
9 665c255d 2023-08-04 jrmu null-value
10 665c255d 2023-08-04 jrmu (combiner (term a)
11 665c255d 2023-08-04 jrmu (accumulate combiner null-value term (next a) next b))))
12 665c255d 2023-08-04 jrmu
13 665c255d 2023-08-04 jrmu (define (sum term a next b)
14 665c255d 2023-08-04 jrmu (accumulate + 0 term a next b))
15 665c255d 2023-08-04 jrmu (define (product term a next b)
16 665c255d 2023-08-04 jrmu (accumulate * 1 term a next b))
17 665c255d 2023-08-04 jrmu
18 665c255d 2023-08-04 jrmu
19 665c255d 2023-08-04 jrmu (define (accumulate combiner null-value term a next b)
20 665c255d 2023-08-04 jrmu (if (> a b)
21 665c255d 2023-08-04 jrmu null-value
22 665c255d 2023-08-04 jrmu (combiner (term a)
23 665c255d 2023-08-04 jrmu (accumulate combiner null-value term (next a) next b))))
24 665c255d 2023-08-04 jrmu
25 665c255d 2023-08-04 jrmu ;; b. If your accumulate procedure generates a recursive process, write one that generates an iterative process. If it generates an iterative process, write one that generates a recursive process.
26 665c255d 2023-08-04 jrmu
27 665c255d 2023-08-04 jrmu (define (accumulate-iter combiner null-value term a next b)
28 665c255d 2023-08-04 jrmu (define (iter i result)
29 665c255d 2023-08-04 jrmu (if (> a b)
30 665c255d 2023-08-04 jrmu result
31 665c255d 2023-08-04 jrmu (iter (next i) (combiner (term i) result))))
32 665c255d 2023-08-04 jrmu (iter a null-value))
33 665c255d 2023-08-04 jrmu
34 665c255d 2023-08-04 jrmu
35 665c255d 2023-08-04 jrmu (define (factorial n)
36 665c255d 2023-08-04 jrmu (product (lambda (x) x)
37 665c255d 2023-08-04 jrmu 1
38 665c255d 2023-08-04 jrmu (lambda (x) (+ x 1))
39 665c255d 2023-08-04 jrmu n))
40 665c255d 2023-08-04 jrmu
41 665c255d 2023-08-04 jrmu (define (factorial-iter n)
42 665c255d 2023-08-04 jrmu (product-iter (lambda (x) x)
43 665c255d 2023-08-04 jrmu 1
44 665c255d 2023-08-04 jrmu (lambda (x) (+ x 1))
45 665c255d 2023-08-04 jrmu n))
46 665c255d 2023-08-04 jrmu
47 665c255d 2023-08-04 jrmu ;; pi/4 = 2*4*4*6*6*8*...
48 665c255d 2023-08-04 jrmu ;; ---------------
49 665c255d 2023-08-04 jrmu ;; 3*3*5*5*7*7*...
50 665c255d 2023-08-04 jrmu
51 665c255d 2023-08-04 jrmu (define (pi iterations)
52 665c255d 2023-08-04 jrmu (* 4.0
53 665c255d 2023-08-04 jrmu (product (lambda (x)
54 665c255d 2023-08-04 jrmu (if (odd? x)
55 665c255d 2023-08-04 jrmu (/ (+ x 1) (+ x 2))
56 665c255d 2023-08-04 jrmu (/ (+ x 2) (+ x 1))))
57 665c255d 2023-08-04 jrmu 1
58 665c255d 2023-08-04 jrmu (lambda (x) (+ x 1))
59 665c255d 2023-08-04 jrmu iterations)))
60 665c255d 2023-08-04 jrmu
61 665c255d 2023-08-04 jrmu (define (test-case actual expected)
62 665c255d 2023-08-04 jrmu (load-option 'format)
63 665c255d 2023-08-04 jrmu (newline)
64 665c255d 2023-08-04 jrmu (format #t "Actual: ~A Expected: ~A" actual expected))
65 665c255d 2023-08-04 jrmu
66 665c255d 2023-08-04 jrmu (test-case (factorial 0) 1)
67 665c255d 2023-08-04 jrmu (test-case (factorial 1) 1)
68 665c255d 2023-08-04 jrmu (test-case (factorial 2) 2)
69 665c255d 2023-08-04 jrmu (test-case (factorial 3) 6)
70 665c255d 2023-08-04 jrmu (test-case (factorial 4) 24)
71 665c255d 2023-08-04 jrmu (test-case (factorial 5) 120)
72 665c255d 2023-08-04 jrmu (test-case (factorial 6) 720)
73 665c255d 2023-08-04 jrmu (test-case (factorial 7) 5040)
74 665c255d 2023-08-04 jrmu (test-case (factorial-iter 7) 5040)
75 665c255d 2023-08-04 jrmu
76 665c255d 2023-08-04 jrmu (test-case (pi 10000) 3.1415)
77 665c255d 2023-08-04 jrmu
78 665c255d 2023-08-04 jrmu
79 665c255d 2023-08-04 jrmu