ITeye博客

SICP 1.31 答案

t0uch · 2009-11-11 19:31 · SICP · Markdown

递归的版本

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

(define (wallis-product n)
  (define (square x)
    (* x x))
  (define (inc a)
    (+ a 2))
  (define (term a)
    (cond ((= a n) 1)
          ((= a 2) (/ (* 2 n) (square (- n 1))))
          (else (/ (square a) (square (- a 1))))))
  (* 4 (product term 2 inc n)))

(wallis-product 10000)

迭代的版本

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。