1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| ;类似于sum的过程product,递归版本 (define (product f a next b) (if (> a b) 1 (* (f a) (product f (next a) next b)))) ;用product定义阶乘过程factorial (define (factorial n) (define (f x) x) (define (next i) (+ i 1)) (product f 1 next n)) ;计算pi值,首先计算每一个分数 (define (fraction i) (if (odd? i) (/ (+ i 1) (+ i 2)) (/ (+ i 2) (+ i 1)))) ;计算pi值,n是精确度 (define (pi n) (define (next i) (+ i 1)) (* (product fraction 1.0 next n) 4.0))
|