問題1.31

僕の解答

(define (product term a next b)
	(if (> a b)
	    1
	  (* (term a)
	     (product term (next a) next b))))

;(define (product term a next b)
;	(define (iter a result)
;		(if (> a b)
;		    result
;		    (iter (next a) (* result (term a)))))
;	(iter a 1))

(define (factorial n)
	(product
	 (lambda (x) x)
	 1
	 (lambda (x) (+ x 1))
	 n))

(define (pi n)
	 (define (term x)
		 (if (even? x)
		     (/ (+ x 2.0) (+ x 3.0))
		     (/ (+ x 3.0) (+ x 2.0))))
	(* (product term 0 (lambda (x) (+ x 1)) n) 4))

(display (factorial 4))
(newline)

(display (pi 1000000))
(newline)

所感

termがかっこ悪い。
lambdaはまあ…便利なので。