1 ;; Exercise 1.30. The sum procedure above generates a linear recursion. The procedure can be rewritten so that the sum is performed iteratively. Show how to do this by filling in the missing expressions in the following definition:
3 (define (product term a next b)
7 (product term (next a) next b))))
9 (define (product-iter term a next b)
10 (define (iter i result)
13 (iter (next i) (* (term i) result))))
17 (product (lambda (x) x)
22 (define (factorial-iter n)
23 (product-iter (lambda (x) x)
28 ;; pi/4 = 2*4*4*6*6*8*...
32 (define (pi iterations)
42 (define (test-case actual expected)
45 (format #t "Actual: ~A Expected: ~A" actual expected))
47 (test-case (factorial 0) 1)
48 (test-case (factorial 1) 1)
49 (test-case (factorial 2) 2)
50 (test-case (factorial 3) 6)
51 (test-case (factorial 4) 24)
52 (test-case (factorial 5) 120)
53 (test-case (factorial 6) 720)
54 (test-case (factorial 7) 5040)
55 (test-case (factorial-iter 7) 5040)
57 (test-case (pi 10000) 3.1415)