SICP 1.31 答案

递归的版本

(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))

你可能感兴趣的:(Scheme)