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:
3 665c255d 2023-08-04 jrmu ;; (accumulate combiner null-value term a next b)
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.
7 665c255d 2023-08-04 jrmu (define (accumulate combiner null-value term a next b)
10 665c255d 2023-08-04 jrmu (combiner (term a)
11 665c255d 2023-08-04 jrmu (accumulate combiner null-value term (next a) next b))))
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))
19 665c255d 2023-08-04 jrmu (define (accumulate combiner null-value term a next b)
22 665c255d 2023-08-04 jrmu (combiner (term a)
23 665c255d 2023-08-04 jrmu (accumulate combiner null-value term (next a) next b))))
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.
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)
31 665c255d 2023-08-04 jrmu (iter (next i) (combiner (term i) result))))
32 665c255d 2023-08-04 jrmu (iter a null-value))
35 665c255d 2023-08-04 jrmu (define (factorial n)
36 665c255d 2023-08-04 jrmu (product (lambda (x) x)
38 665c255d 2023-08-04 jrmu (lambda (x) (+ x 1))
41 665c255d 2023-08-04 jrmu (define (factorial-iter n)
42 665c255d 2023-08-04 jrmu (product-iter (lambda (x) x)
44 665c255d 2023-08-04 jrmu (lambda (x) (+ x 1))
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*...
51 665c255d 2023-08-04 jrmu (define (pi iterations)
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))))
58 665c255d 2023-08-04 jrmu (lambda (x) (+ x 1))
59 665c255d 2023-08-04 jrmu iterations)))
61 665c255d 2023-08-04 jrmu (define (test-case actual expected)
62 665c255d 2023-08-04 jrmu (load-option 'format)
64 665c255d 2023-08-04 jrmu (format #t "Actual: ~A Expected: ~A" actual expected))
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)
76 665c255d 2023-08-04 jrmu (test-case (pi 10000) 3.1415)