(define (product-recursive factor a next b) (if (> a b) 1 (* (factor a) (product-recursive factor (next a) next b)))) (define (product-iterative factor a next b) (define (iter a result) (if (> a b) result (iter (next a) (* (factor a) result)))) (iter a 1)) (define (identity x) x) (define (inc x) (+ x 1)) (define (factorial n) (product-iterative identity 1 inc n)) ; 1 ]=> (map factorial (iota 10)) ; ; ;Value: (1 1 2 6 24 120 720 5040 40320 362880) (define (approximate-pi n) ; Returns smallest even integer >= n (define (first-even n) (if (even? n) n (inc n))) ; Returns smallest odd integer >= n (define (first-odd n) (if (even? n) (inc n) n)) (define (term n) (/ (+ 0.0 (first-even n)) ; hack to convert to floating point (first-odd n))) (define end (inc n)) (* 4 (product-iterative term 2 inc end))) ; 1 ]=> (map approximate-pi '(1 10 100 1000 10000 100000 1000000)) ; ; ;Value: (2.6666666666666665 3.2751010413348065 3.1570301764551654 3.1431607055322552 3.1417497057380084 3.141608361277941 3.141594224382854)