通用求和过程
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
下面几题答案,非本人解答,有待验证
练习1.29
函数f在范围a和b之间的定积分的近似值
(h/3)[y
0
+ 4y
1
+ 2y
2
+ 4y
3
+ 2y
4
+ …… + 2y
n−2
+ 4y
n−1
+ y
n
]
h = (b - a) / n, n为某个偶数
y
k
=
f
<script id="MathJax-Element-360" type="math/tex">f</script>(a + kh) 增大n能提高近似值的精度
(define (si f a b n)
(let ((h (/ (- b a) n)))
(define (next x)
(+ x 1))
(define (yk k)
(f (+ a (* k h))))
(* (+ (sum yk 1 next (- n 1))
(yk 0)
(yk n))
(/ h 3))))
(define (sum term a ne b)
(if (> a b)
0
(+ (* (if (even? a) 2 4)
(term a))
(sum term (ne a) ne b))))
练习1.30
(define (si f a b n)
(let ((h (/ (- b a) n)))
(define (next x)
(+ x 1))
(define (yk k)
(f (+ a (* k h))))
(* (+ (sum yk 1 next (- n 1))
(yk 0)
(yk n))
(/ h 3))))
(define (sum term a ne b)
(define (iter c result)
(if (> c b)
result
(iter (ne c) (+ result (* (if (even? c) 2 4) (term c))))))
(iter a 0))
(define (cube x)
(* x x x))
练习1.31
(define (product term a next b)
(if (> a b)
1
(* (term a)
(product term (next a) next b))))
(define (factorial n)
(define (te x)
x)
(define (ne x)
(+ x 1))
(product te 1 ne n))
(define (pi n)
(define (te x)
(/ (* (- x 1) (+ x 1))
(* x x)))
(define (ne x)
(+ x 2))
(* (product te 3 ne (+ n 2))
4.0))
练习1.32
(define (accumulate_r combiner null-value term a next b)
(if (> a b)
null-value
(combiner (term a)
(accumulate_r combiner null-value term (next a) next b))))
(define (accumulate_i combiner null-value term a next b result)
(if (> a b)
result
(accumulate_i combiner null-value term (next a) next b (combiner result (term a)))))
(define (sum term a next b)
(define (plus x y)
(+ x y))
(accumulate_r plus 0 term a next b))
(define (product term a next b)
(define (multiply x y)
(* x y))
(accumulate_i multiply 1 term a next b 1))
练习1.33
(define (filtered-accumulate_r combiner null-value term a next b f)
(if (> a b)
null-value
(if (f a)
(combiner (term a)
(accumulate_r combiner null-value term (next a) next b f))
(accumulate_r combiner null-value term (next a) next b f))))
(define (sum-of-squres-of-prime a b)
(define (combiner x y)
(+ x y))
(define (term x)
(* x x))
(define (next x)
(+ x 1))
(filtered-accumulate_r combiner 0 term a next b prime?))
(define (product-of-primes-to-n n)
(define (combiner x y)
(* x y))
(define (term x)
x)
(define (next x)
(+ x 1))
(define (prime-to-n? x)
(= (gcd x n) 1))
(filtered-accumulate_r combiner 1 term a next b prime-to-n?))