灯下 登录
计算机科学 / SICP / 3.3.5 Propagation of Constraints

Exercise 3.35 · 习题

Exercise 3.35: Ben Bitdiddle tells Louis that
one way to avoid the trouble in Exercise 3.34 is to define a squarer as a
new primitive constraint. Fill in the missing portions in Ben’s outline for a
procedure to implement such a constraint:

(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))

⟨alternative1⟩)

⟨alternative2⟩))

(define (process-forget-value) ⟨body1⟩)

(define (me request) ⟨body2⟩)

⟨rest of definition⟩

me)

练习 3.35:Ben Bitdiddle 告诉 Louis,避免练习 3.34 中问题的一种方法是将平方器定义为一种新的基本约束。请填写 Ben 为实现此约束所给出的过程框架中缺失的部分:

(define (squarer a b)
(define (process-new-value)
(if (has-value? b)
(if (< (get-value b) 0)
(error “square less than 0:\nSQUARER”
(get-value b))
⟨alternative1⟩)
⟨alternative2⟩))
(define (process-forget-value) ⟨body1⟩)
(define (me request) ⟨body2⟩)
⟨rest of definition⟩
me)

Racket #lang sicp
(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))
 ⟨alternative1⟩)
 ⟨alternative2⟩))
 (define (process-forget-value) ⟨body1⟩)
 (define (me request) ⟨body2⟩)
 ⟨rest of definition⟩
 me)