SICP Exercise 3.35

SICP Exercise 3.35

;;;Exercise 3.35
(define (squarer a b)
  (define (process-new-value)
    (if (has-value? b)
        (if (< (get-value b) 0)
            (error "square less than 0 -- SQUARER" (get-value b))
            (set-value! a (sqrt (get-value b)) me))
        (if (has-value? a)
            (set-value! b (* (get-value a) (get-value a)) me))))
  (define (process-forget-value)
    (forget-value! a me)
    (forget-value! b me)
    (process-new-value))
  (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 -- SQUARER" request))))
  (connect a me)
  (connect b me)
  me)

The test code is :

;test squarer
(define a (make-connector))
(define b (make-connector))
(probe 'a a)
(probe 'b b)
(define square (squarer a b))
(set-value! a 2 'user)
(forget-value! a 'user)
(set-value! b 2 'user)
The output of test is:

#<procedure:me>
#<procedure:me>
Probe: b = 4
Probe: a = 2
'done
Probe: b = ?
Probe: a = ?
'done
Probe: a = 1.4142135623730951
Probe: b = 2
'done


你可能感兴趣的:(user,less,output)