Code Review
Compare your solutions
(define (filtered-accumulate combiner null-value term a next b filter?)
(define (iter a result)
(if (> a b)
result
(iter (next a) (if (filter? a)
(combiner (term a) result)
result))))
(iter a null-value))
; (define (filtered-accumulate combiner null-value term a next b filter?)
; (if (> a b)
; null-value
; (if (filter? a)
; (combiner (term a)
; (filtered-accumulate combiner null-value term (next a) next b filter?))
; (filtered-accumulate combiner null-value term (next a) next b filter?))))
(define (sum a b) (filtered-accumulate + 0 identity a inc b identity))
(define (prod a b) (filtered-accumulate * 1 identity a inc b identity))
(define (prime-square-sum a b) (filtered-accumulate + 0 square a inc b prime?))
(define (b-prime-product b)
(define (is-prime-for-b x) (= (gcd b x) 1))
(filtered-accumulate * 1 identity 1 inc b is-prime-for-b))
(define (smallest-divisor n)
(define (find-divisor test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor (+ test-divisor 1)))))
(find-divisor 2))
(define (divides? a b)
(= (remainder b a) 0))
(define (prime? n)
(= n (smallest-divisor n)))
(define (gcd a b)
(if (= b 0)
a
(gcd b (remainder a b))))