`
t0uch
  • 浏览: 56831 次
  • 性别: Icon_minigender_1
  • 来自: 柳州
社区版块
存档分类
最新评论

SICP 1.31 答案

    博客分类:
  • SICP
阅读更多
递归的版本

(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))
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics