Chat (Lingr.com)
Informaiton
Daily
Column
- MySQL日本語の旅(5/1)
- アクセス向上秘伝(5/9)
- 一風変ったHaskellλ門(6/13)
- SICP Answer Book (5/31) 問題3.26追加
Zope Solution
Extra
アーカイブ
OSS案内所
Site Info
関連リンク
##(link2sicp "book-Z-H-12.html#%_thm_1.33" "Exercise 1.33")
解答例
再帰プロセス版 filtered-accumulate
(define (filtered-accumulate combiner null-value term filter a next b)
(cond ((> a b) null-value)
((filter a)
(combiner
(term a)
(filtered-accumulate
combiner null-value term filter (next a) next b)))
(else
(filtered-accumulate
combiner null-value term filter (next a) next b))))
反復プロセス版 filtered-accumulate
(define (filtered-accumulate combiner null-value term filter a next b)
(cond ((> a b) null-value)
((filter a)
(filtered-accumulate
combiner (combiner null-value (term a)) term filter (next a) next b))
(else
(filtered-accumulate
combiner null-value term filter (next a) next b))))
a. 区間 a, b の素数の二乗の和
(define (sum-of-square-primes a b) (filtered-accumulate + 0 square prime? a inc b))
b. n と互いに素で、n より小さい正の整数の積
(define (product-of-irreducibles n)
(define (primeWith? n)
(lambda (x) (= (gcd n x) 1)))
(filtered-accumulate * 1 identity (primeWith? n) 1 inc (- n 1)))
コード
##(sicp-answer-code "ex-1.33.scm")