さかもとのブログ

つらつらと

SICP演習問題3.29の確認と3.31

  • 3.29の確認
;;通常のor-gate
gosh> (load "./section3-3-4.scm")

sum 0 New-value = 0

carry 0 New-value = 0

sum 8 New-value = 1

carry 11 New-value = 1

sum 16 New-value = 0
#t

;;exercize3.29のor gate
gosh> (load "./section3-3-4.scm")

sum 0 New-value = 0

carry 0 New-value = 0

sum 5 New-value = 1

sum 10 New-value = 0

sum 10 New-value = 1

carry 13 New-value = 1

sum 18 New-value = 0
#t

問題なく動き,時間の差がでている.

  • 3.31

make-wireの内部手続きである,accept-acction-procedure!を

(define (accept-action-procedure! proc)
   (set! action-procedures (cons proc action-procedures)))

と定義するということは,inverter, or-gate, and-gateにおいて,afer-delayが実行されない.
つまり,add-to-agenda!が実行されず,agendaに手続きが追加されない.

であっているはずなんだけど,実際に実行してみると,

carry 11 New-value = 1

という結果が返ってくる.これはなんだ?なにも追加されないのに,carryの遅延時間や,値は計算されている.
probeに

(print the-agenda)

を追加して,両方のパターンを実行してみると,

;;初期化する版
gosh> (load "./section3-3-4.scm")

(0)
sum 0 New-value = 0

(0)
carry 0 New-value = 0

(8 (8 (#<closure (and-gate and-action-procedure #f)>) #<closure (and-gate and-action-procedure #f)>))
sum 8 New-value = 1

(11 (11 (#<closure (and-gate and-action-procedure #f)>) #<closure (and-gate and-action-procedure #f)>) (13 (#<closure (or-ga\
te or-action-procedure #f)> #<closure (inverter invert-input #f)>) #<closure (inverter invert-input #f)>))
carry 11 New-value = 1

(16 (16 (#<closure (and-gate and-action-procedure #f)>) #<closure (and-gate and-action-procedure #f)>))
sum 16 New-value = 0
#t

;初期化しない版
gosh> (load "./section3-3-4.scm")

(11 (11 (#<closure (and-gate and-action-procedure #f)>) #<closure (and-gate and-action-procedure #f)>) (13 (#<closure (or-ga\
te or-action-procedure #f)> #<closure (inverter invert-input #f)>) #<closure (inverter invert-input #f)>))
carry 11 New-value = 1
#t

carryに関するところはまったく同じagendaである.
うーむよくわからない.