問題1.33
ここまでくると、コードから意味を読み取るのは大変。馴れが重要なのかな?
filtered-accumulate
(define (filtered-accumulate filter combiner null-value term a next b) (if (> a b) null-value (if (filter a) (combiner (term a) (filtered-accumulate filter combiner null-value term (next a) next b)) (filtered-accumulate filter combiner null-value term (next a) next b)))) ; 階乗 (define (factorial n) (filtered-accumulate (lambda (x) #t) * 1 (lambda (x) x) 1 (lambda (x) (+ x 1)) n)) ; 素数 (define (prime-square a b) (filtered-accumulate prime? + 0 square a (lambda (n) (+ n 1)) b)) ; GCD (define (gcd-product i n) (filtered-accumulate gcd-one? * 1 (lambda (x) x) i (lambda (n) (+ n 1)) n))