1 (define (sum term a next b)
5 (sum term (next a) next b))))
7 ;; (define (simpsons-rule f a b n)
8 ;; (let ((h (/ (- b a) n)))
9 ;; (define (running-sum k)
10 ;; (let ((akh (+ a (* k h))))
13 ;; (+ (cond ((= k 0) (f akh))
14 ;; ((even? k) (* 2 (f akh)))
15 ;; ((odd? k) (* 4 (f akh))))
16 ;; (running-sum (+ k 1))))))
20 (define (simpsons-rule f a b n)
21 (let ((h (/ (- b a) n)))
22 (define (simpsons-term k)
23 (let ((akh (+ a (* k h))))
25 (cond ((or (= k 0) (= k n)) 1)
29 (sum simpsons-term 0 1+ n))))
32 (define (test-case actual expected)
35 (format #t "Actual: ~A Expected: ~A" actual expected))
37 (define (cube x) (* x x x))
39 (test-case (simpsons-rule cube 0.0 1.0 5) 0.25)
40 (test-case (simpsons-rule cube 0.0 1.0 10) 0.25)
41 (test-case (simpsons-rule cube 0.0 1.0 100) 0.25)