問題1.32

僕の解答

こんな感じ。

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

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

(define (sum-integers a b)
	(sum (lambda (x) x) a (lambda (x) (+ x 1)) b))

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

(define (product-integers a b)
	(product (lambda (x) x) a (lambda (x) (+ x 1)) b))

(display (sum-integers 1 10))
(newline)
(display (product-integers 1 5))
(newline)

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

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

(define (sum-integers-b a b)
	(sum-b (lambda (x) x) a (lambda (x) (+ x 1)) b))

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

(define (product-integers-b a b)
	(product-b (lambda (x) x) a (lambda (x) (+ x 1)) b))

(display (sum-integers-b 1 10))
(newline)
(display (product-integers-b 1 5))
(newline)

所感

反復版は、null-valueを増やしていけばいいのかぁ。