(define(accumulate combiner null-value term a next b)(if(> a b)
null-value
(combiner(term a)(accumulate combiner null-value term (next a) next b))))(define(sum term a next b)(define(combiner a1 a2)(+ a1 a2))(accumulate combiner 0 term a next b))(define(product term a next b)(define(combiner a1 a2)(* a1 a2))(accumulate combiner 1 term a next b))(define(simpson f a b n)(define h (/(- b a) n))(define(new-f n1)(cond((or(= n10)(= n1 n))(f(+ a (* n1 h))))((even? n1)(*2(f(+ a (* n1 h)))))(else(*4(f(+ a (* n1 h)))))))(define(inc n)(+ n 1))(*(/ h 3)(sum new-f 0 inc n)))(newline)(display(simpson cube 01100))(define(calc-pi n)(define(even? n)(=(remainder n 2)0))(define(f n)(if(even? n)(/ n (+ n 1))(/(+ n 1) n)))(define(inc n)(+ n 1))(*4.0(product f 2 inc n)))(newline)(display(calc-pi1000))
迭代
(define(accumulate combiner null-value term a next b)(define(iter a result)(if(> a b)
result
(iter(next a)(combiner result (term a)))))(iter a null-value))(define(sum term a next b)(define(combiner a1 a2)(+ a1 a2))(accumulate combiner 0 term a next b))(define(product term a next b)(define(combiner a1 a2)(* a1 a2))(accumulate combiner 1 term a next b))(define(simpson f a b n)(define h (/(- b a) n))(define(new-f n1)(cond((or(= n10)(= n1 n))(f(+ a (* n1 h))))((even? n1)(*2(f(+ a (* n1 h)))))(else(*4(f(+ a (* n1 h)))))))(define(inc n)(+ n 1))(*(/ h 3)(sum new-f 0 inc n)))(newline)(display(simpson cube 01100))(define(calc-pi n)(define(even? n)(=(remainder n 2)0))(define(f n)(if(even? n)(/ n (+ n 1))(/(+ n 1) n)))(define(inc n)(+ n 1))(*4.0(product f 2 inc n)))(newline)(display(calc-pi1000))
递归(define (accumulate combiner null-value term a next b) (if (> a b) null-value (combiner (term a) (accumulate combiner null-value term (next a) next b))))(define (sum term a next b) (defin