SICP:为什么进程忘记值调用进程 - 新值?

时间:2014-10-05 10:08:32

标签: scheme lisp constraints sicp

此代码来自SICP,3.3.5 Propagation of Constraints。我无法弄清楚为什么process-forget-value需要将process-new-value称为最后一步。

文字说,"最后一步的原因是一个或多个连接器可能仍然有一个值(也就是说,连接器可能有一个最初不是由加法器设置的值),这些值可能需要通过加法器传播回来。"

什么是最简单的约束网络,可以证明为什么需要(process-new-value)?谢谢!

(define (adder a1 a2 sum)
  (define (process-new-value)
    (cond ((and (has-value? a1) (has-value? a2))
           (set-value! sum
                       (+ (get-value a1) (get-value a2))
                       me))
          ((and (has-value? a1) (has-value? sum))
           (set-value! a2
                       (- (get-value sum) (get-value a1))
                       me))
          ((and (has-value? a2) (has-value? sum))
           (set-value! a1
                       (- (get-value sum) (get-value a2))
                       me))))

  (define (process-forget-value)
    (forget-value! sum me)
    (forget-value! a1 me)
    (forget-value! a2 me)
    (process-new-value))  ;;; * WHY * ???

  (define (me request)
    (cond ((eq? request 'I-have-a-value)  
           (process-new-value))
          ((eq? request 'I-lost-my-value) 
           (process-forget-value))
          (else 
           (error "Unknown request -- ADDER" request))))

  (connect a1 me)
  (connect a2 me)
  (connect sum me)
  me)

1 个答案:

答案 0 :(得分:2)

这是我从加法器中删除process-new-value的测试。您将看到行为不同。

(define c (make-connector))
(define a (make-connector))
(define b (make-connector))
(define d (make-connector))

(constant 10 a)
(constant 10 c)
(constant 10 d)

(define adder1 (adder a b c))
(define adder2 (adder a b d))

> (has-value? b)
#t

> (get-value b)
0

> (forget-value! b adder1)
'done    

> (has-value? b)
#f

如果使用正确的版本执行此操作。

> (has-value? b)
#t

第二次也是。正如他们所说,当adder1告诉b忘记其价值时。 ac作为常量仍会有一个值,process-new-value中的最后adder2将再次将b设置为0.如果您将set-value!用于ac