Solution to SICP Exercise 1.32

Structure and Interpretation of Computer Programs

Solution to Exercise 1.32:

; part a (recursive process)

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

(define (sum-r term a next b)
(accumulate-r + 0 term a next b))

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

; part b (iterative process)

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

Advertisements