Open Source WEB

##(link2sicp "book-Z-H-12.html#%_thm_1.31" "Exercise 1.31")

解答例

a. 再帰プロセス版 product

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

b. 反復プロセス版 product

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

factorial

(define (factorial n)
  (define (term i) i)
  (define (next i) (+ i 1))
  (product term 1 next n))

Wallis PI

(define (wallis-pi n)
  (define (term i)
    (/ (* (* 2 i) (* 2 (+ i 1)))
       (square (+ (* 2 i) 1))))
  (define (next i) (+ i 1))
  (* 4 (product term 1 next n)))

コード

##(sicp-answer-code "ex-1.31.scm")

このサイトは、 IPA の「平成15年度オープンソフトウエア活用基盤整備事業」 の委託事業として開発されたKahuaで試験的に運用しております。

Copyright (c) 2004-2007 株式会社タイムインターメディア About Us