constraint - ; Programming with constraints, from section...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
;; Programming with constraints, from section 3.3.5 of Abelson and Sussman. ;; Syntactic interface to contraint and probe objects. ;; These operations inform them that a value has become defined or undefined; ;; they have to figure out which value is involved. (define (inform-about-value constraint) ((constraint 'I-have-a-value))) (define (inform-about-no-value constraint) ((constraint 'I-lost-my-value))) ;; Types of constraints defined here: adder, multiplier and constant; ;; also define probe, which is a pseudo-constraint. (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)) (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) (define (multiplier m1 m2 product) (define (process-new-value) (cond ((or (if (has-value? m1) (= (get-value m1) 0) #f)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

constraint - ; Programming with constraints, from section...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online