sicp-3-3-5
Posted on 2015-03-01 22:37:18 +0900
in SICP
Lisp
3.33
( define ( average a b c )
( let (( x ( make-connector ))
( y ( make-connector )))
( adder a b x )
( multiplier c v u )
( constant 2 v ))
'ok )
3.34
it is only one-directianl computation. Given the value of b
, the value of
a
can not be evaluated, as the multiplier does not know the left two values should be the same.
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 ( square ( get-value a )) me ))))
( define ( process-forget-value )
( forget-value! b me )
( forget-value! a me ))
( define ( me request )
( cond (( eq? request 'I-have-a-value )
( process-new-value ))
(( eq? request 'I-lost-my value )
( process-new-value ))
( else
( error "Unkown request SQUARE" request ))))
( connect a me )
( connect b me )
me )
3.37
( 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 x )
( let (( z ( make-connector )))
( constant x z )
z ))