SICP Exercise 3.33
Question
Using primitive multiplier
, adder
, and constant
constraints, define a
procedure averager
that takes three connectors a
, b
, and c
as inputs and
establishes the constraint that the value of c
is the average of the values of
a
and b
.
Answer
Here’s the constraint network for this scenario:
And the equations: \[a=2c-b\] \[b=2c-a\] \[c=\frac{a+b}{2}\]
And finally the code:
(define (averager a b c)
(let ((u (make-connector))
(v (make-connector)))
(adder a b u)
(constant 2 v)
(multiplier c v u)
'ok))