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-20.html#%_thm_3.2" "Exercise 3.2")
解答例
(define (make-monitored func)
(define mf 0)
(define (how-many-calls?) mf)
(define (reset-count) (set! mf 0) mf)
(define (dispatch m)
(cond ((eq? m 'how-many-calls?) (how-many-calls?))
((eq? m 'reset-count) (reset-count))
(else (set! mf (+ 1 mf))
(func m))))
dispatch)
実行例
gosh> (define s (make-monitored sqrt)) s gosh> (s 'how-many-calls?) 0 gosh> (s 100) 10.0 gosh> (s 'how-many-calls?) 1 gosh> (s 9) 3.0 gosh> (s 'how-many-calls?) 2 gosh> (s 2500) 50.0 gosh> (s 'how-many-calls?) 3 gosh> (s 'reset-count) 0 gosh> (s 'how-many-calls?) 0 gosh> (s 100) 10.0 gosh> (s 'how-many-calls?) 1
--hidenao
コード
##(sicp-answer-code "ex-3.2.scm")