Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

constraints

  • Loading branch information...
commit 965c160851ca223839c40f2db205197fba45b7cc 1 parent 568151d
@sarabander authored
Showing with 154 additions and 0 deletions.
  1. +39 −0 3.3/3.33.scm
  2. +28 −0 3.3/3.34.scm
  3. +41 −0 3.3/3.35.scm
  4. +46 −0 3.3/3.37.scm
View
39 3.3/3.33.scm
@@ -0,0 +1,39 @@
+
+;; Depends on definitions in ch3.scm, section 3.3.5
+
+(define a (make-connector))
+(define b (make-connector))
+(define c (make-connector))
+
+(define (averager a b c)
+ (let ((s (make-connector))
+ (h (make-connector)))
+ (adder a b s)
+ (multiplier s h c)
+ (constant 1/2 h)
+ 'ok))
+
+(averager a b c)
+
+(probe "a" a)
+(probe "b" b)
+(probe "c" c)
+
+(set-value! a 10 'user) ; Probe: a = 10
+(set-value! b 14 'user) ; Probe: c = 12, Probe: b = 14
+(get-value c) ; 12
+
+(forget-value! a 'user)
+(forget-value! b 'user)
+(forget-value! c 'user)
+
+(set-value! a 5 'user) ; Probe: a = 5
+(set-value! c 9 'user) ; Probe: b = 13, Probe: c = 9
+(get-value b) ; 13
+
+(forget-value! a 'user)
+(forget-value! b 'user)
+(forget-value! c 'user)
+
+(set-value! c 4.7 'user) ; Probe: c = 4.7
+(set-value! b -3.3 'user) ; Probe: a = 12.7, Probe: b = -3.3
View
28 3.3/3.34.scm
@@ -0,0 +1,28 @@
+
+(define (squarer a b)
+ (multiplier a a b))
+
+(define e (make-connector))
+(define f (make-connector))
+
+(squarer e f)
+
+(probe "e" e)
+(probe "f" f)
+
+(set-value! e 10 'user) ; Probe: e = 10, Probe: f = 100
+(get-value f) ; 100
+;; Right!
+
+(forget-value! e 'user)
+(forget-value! f 'user)
+
+(set-value! f 144 'user) ; Probe: f = 144
+(get-value e) ; 10
+;; Wrong!
+
+;; Previous value of e stays in the input. If this were the first
+;; invocation, e would be #f.
+
+;; The squarer works only in one direction, there's no way to produce
+;; square roots by a single division operation.
View
41 3.3/3.35.scm
@@ -0,0 +1,41 @@
+
+(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 (square (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)
+
+(define e (make-connector))
+(define f (make-connector))
+
+(squarer e f)
+
+(probe "e" e)
+(probe "f" f)
+
+(set-value! e 10 'user) ; Probe: e = 10, Probe: f = 100
+;; Right!
+
+(forget-value! e 'user)
+(forget-value! f 'user)
+
+(set-value! f 625 'user) ; Probe: f = 625, Probe: e = 25
+;; Right!
View
46 3.3/3.37.scm
@@ -0,0 +1,46 @@
+
+(define (celsius-fahrenheit-converter x)
+ (c+ (c* (c/ (cv 9) (cv 5))
+ x)
+ (cv 32)))
+
+(define C (make-connector))
+(define F (celsius-fahrenheit-converter C))
+
+(define (c+ x y)
+ (let ((z (make-connector)))
+ (adder x y z)
+ z))
+
+(define (c* x y)
+ (let ((z (make-connector)))
+ (multiplier x y z)
+ z))
+
+(define (c/ x y)
+ (let ((z (make-connector)))
+ (multiplier y z x)
+ z))
+
+(define (cv const)
+ (let ((z (make-connector)))
+ (constant const z)
+ z))
+
+(probe "Celsius temp" C)
+(probe "Fahrenheit temp" F)
+
+(set-value! C 0 'user)
+; Probe: Celsius temp = 0
+; Probe: Fahrenheit temp = 32
+
+(forget-value! C 'user)
+(set-value! F 100 'user)
+; Probe: Fahrenheit temp = 100
+; Probe: Celsius temp = 340/9
+(/ 340 9.) ; 37.78
+
+(forget-value! F 'user)
+(set-value! F 98.6 'user)
+; Probe: Fahrenheit temp = 98.6
+; Probe: Celsius temp = 37
Please sign in to comment.
Something went wrong with that request. Please try again.