1.33|

$ cd ~

SICP Exercise 1.33

The procedure filtered-accumulator:

(define (filtered-accumulate combiner null-value term a next b filter) 
 (if (> a b) 
  null-value 
  combiner (if (filter a) 
               (term a) 
                null-value) 
           (filtered-accumulate combiner null-value term (next a) next b filter)))

Part A: Borrowing prime? from earlier:

(define (sum-of-prime-squares a b)
    (filtered-accumulate + 0 square a inc b prime?))

Part B:

(define (gcd m n)
  (cond ((< m n) (gcd (n m)))
        ((= n 0) m)
        (else (gcd n (remainder m n)))))

(define (relatively-prime? m n)
  (= (gcd m n) 1))

(define (product-of-relative-primes n) 
   (define (filter x) 
     (relative-prime? x n))
  (filtered-accumulate * 1 identity 1 inc n filter))