Open Source WEB

##(link2sicp "book-Z-H-12.html#%_thm_1.32" "Exercise 1.32")

解答例

sum と product

(define (sum term a next b)
  (accumulate + 0 term a next b))

(define (product term a next b)
  (accumulate * 1 term a next b))

再帰プロセス版 accumulate

(define (accumulate combiner null-value term a next b)
  (if (> a b)
      null-value
      (combiner (term a)
                (accumulate combiner null-value term (next a) next b))))

反復プロセス版 accumulate

(define (accumulate combiner null-value term a next b)
  (if (> a b)
      null-value
      (accumulate combiner (combiner null-value (term a)) term (next a) next b)))

コード

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

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

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