SICP問題1.31
線形再帰な product
(define (product term a next b) (if (> a b) 1 (* (term a) (product term (next a) next b))))
反復手続きな product
(define (product term a next b) (define (iter a result) (if (> a b) result (iter (next a) (* (term a) result)))) (iter a 1))
product を使用した factorial の定義
(define (factorial n) (define (term i) i) (define (next i) (+ i 1)) (product term 1 next n) )
productを用いたπの近似値計算
(define (product-pai n) (define (term i) (if (even? i) (/ (+ i 2) (+ i 1)) (/ (+ i 1) (+ i 2)))) (define (next i) (+ i 1)) (* (product term 1 next n) 4.0) )