# SICP 1.3 exercises cont.

**Posted:**January 20, 2013

**Filed under:**Improving, SICP Leave a comment

**1.31
**

(define (sum-2 term a next b) (define (iter a result) (if (> a b) result (iter (next a) (+ (term a) result)))) (iter a 0)) (define (product term a next b) (if (> a b) 1 (* (term a) (product term (next a) next b)))) (define (product-2 term a next b) (define (iter a result) (if (> a b) result (iter (next a) (* (term a) result)))) (iter a 1)) (define (factorial n) (product-4 identity 1 inc n)) (define (pi-approx n) (define (pi-term x) (if (even? x) (/ (+ 2 x) (+ 1 x)) (/ (+ 1 x) (+ 2 x)))) (* 4.0 (product-4 pi-term 1 inc n)))

**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-3 term a next b) (accumulate + 0 term a next b)) (define (product-3 term a next b) (accumulate * 1 term a next b)) (define (accumulate-2 combiner null-value term a next b) (define (iter a result) (if (> a b) result (iter (next a) (combiner (term a) result)))) (iter a null-value)) (define (sum-4 term a next b) (accumulate-2 + 0 term a next b)) (define (product-4 term a next b) (accumulate-2 * 1 term a next b))

**Comments:** These exercises were quite straightforward because they step the you through everything. It’s basically all fill-in-the-blanks. Which is nice, as I would probably have been lost otherwise.

Advertisements