Solution to Exercise 1.31:

`; part a (recursive process)`

(define (product-r term a next b)

(if (> a b)

1

(* (term a)

(product-r term (next a) next b))))

```
```(define (identity i) i)

(define (inc i) (+ i 1))

(define (factorial n)

(product-r identity 1 inc n))

(define (pi-approximator terms)

(define (numerator i)

(+ i (if (odd? i) 1 2)))

(define (denominator i)

(+ i (if (odd? i) 2 1)))

(define (pi-term i)

(/ (numerator i) (denominator i)))

(* 4.0 (product-r pi-term 1 inc terms)))

`; part b (iterative process)`

(define (product-i term a next b)

(define (iter a result)

(if (> a b)

result

(iter (next a) (* result (term a)))))

(iter a 1))

Advertisements