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)) (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n test-divisor) (cond ((> (square test-divisor) n) n) ((divides? n test-divisor) test-divisor) (else (find-divisor n (+ test-divisor 1))))) (define (divides? a b) (= (remainder a b) 0)) (define (prime? n) (if (< n 2) #f (= n (smallest-divisor n)))) (define (square x) (* x x)) (define (primes-sum a b) (filtered-accumulate prime? + 0 (lambda (x) x) a (lambda (x) (+ x 1)) b)) (primes-sum 1 10)