Blame


1 665c255d 2023-08-04 jrmu ;; 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:
2 665c255d 2023-08-04 jrmu
3 665c255d 2023-08-04 jrmu (define (sum term a next b)
4 665c255d 2023-08-04 jrmu (define (iter a result)
5 665c255d 2023-08-04 jrmu (if (> a b)
6 665c255d 2023-08-04 jrmu result
7 665c255d 2023-08-04 jrmu (iter (next a) (+ (term a) result))))
8 665c255d 2023-08-04 jrmu (iter a 0))
9 665c255d 2023-08-04 jrmu
10 665c255d 2023-08-04 jrmu (define (integral f a b dx)
11 665c255d 2023-08-04 jrmu (define (add-dx x) (+ x dx))
12 665c255d 2023-08-04 jrmu (* (sum f (+ a (/ dx 2.0)) add-dx b)
13 665c255d 2023-08-04 jrmu dx))
14 665c255d 2023-08-04 jrmu
15 665c255d 2023-08-04 jrmu (define (cube x) (* x x x))
16 665c255d 2023-08-04 jrmu
17 665c255d 2023-08-04 jrmu (define (test-case actual expected)
18 665c255d 2023-08-04 jrmu (load-option 'format)
19 665c255d 2023-08-04 jrmu (newline)
20 665c255d 2023-08-04 jrmu (format #t "Actual: ~A Expected: ~A" actual expected))
21 665c255d 2023-08-04 jrmu
22 665c255d 2023-08-04 jrmu
23 665c255d 2023-08-04 jrmu
24 665c255d 2023-08-04 jrmu (test-case (integral cube 0.0 1.0 0.001) 0.25)