练习1.31-练习1.34

(accumulate combiner null-value term a next b)

1.练习1.31

a）

(define (product-recur term a next b)
(if (> a b)
1
(* (term a)
(product-recur term (next a) next b))))

(define (factorial-recur n)
(define (self n)
n)
(define (plus-1 n)
(+ n 1))
(if (= n 0)
1
(product-recur self 1 plus-1 n)))

(define (pi-recur n)
(define (even? n)
(= (remainder n 2) 0))
(define (item n)
(if (even? n)
(/ (+ 2 n) (+ 1 n))
(/ (+ 1 n) (+ 2 n))))
(define (next-1 n)
(+ n 1.0))
(* 4.0
(product-recur item 1.0 next-1 n)))

b）

(define (product-iter term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* (term a) result))))
(iter a 1))

2.练习1.32

a）

(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-accumulate term a next b)
(accumulate + 0 term a next b))

(define (product-accumulate term a next b)
(accumulate-iter * 1 term a next b))

b）

(define  (accumulate-iter 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))

3.练习1.33

(define (filtered-accumulate filter combiner null-value term a next b)
(define (iter a result)
(cond ((> a b) result)
((filter a) (iter (next a) (combiner (term a) result)))
(else (iter (next a) result))))
(iter a null-value))

a）

(define (prime-sum a b)
(define (next-1 a)
(+ a 1))
(define (self a)
a)
(filtered-accumulate prime? + 0 self a next-1 b))

b）

(define (GCD-1-product n)
(define (GCD-1-n? a)
(= (GCD a n) 1))
(define (next-1 a)
(+ a 1))
(define (self a)
a)
(filtered-accumulate GCD-1-n? * 1 self 1 next-1 (- n 1)))

4.练习1.34

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客