(define (product term a next b) (if (> a b) 1 (* (term a) (product term (next a) next b))))
(product (lambda (x) x) 1 (lambda (i) (+ i 1)) 5)
(define (product term a next b) (define (iter a result) (if (> a b) result (iter (next a) (* (term a) result)))) (iter a 1)) (define (product-normal term a next b) (if (> a b) 1 (* (term a) (product term (next a) next b)))) (define (factorial n) (product (lambda (x) x) 1 (lambda (i) (+ i 1)) n)) (define (numer-term i) (cond ((= i 1) 2) ((even? i) (+ i 2)) (else (+ i 1)))) (define (denom-term i) (if (odd? i) (+ i 2) (+ i 1))) (define (pi n) (* 4 (exact->inexact ; exact->inexact 函数转换除法的商,确保计算所得的结果为浮点数格式(而不是分数格式) (/ (product numer-term 1 (lambda (i) (+ i 1)) n) (product denom-term 1 (lambda (i) (+ i 1)) n))))) (pi 50000)