SICP Exercise 3.34
Question
Louis Reasoner wants to build a squarer, a constraint device with two terminals such that the value of connector b on the second terminal will always be the square of the value a on the first terminal. He proposes the following simple device made from a multiplier:
(define (squarer a b) (multiplier a a b))
There is a serious flaw in this idea. Explain.
Answer
The squarer implemented by Louis can actually square a number, as can be
verified by running the code. However, it cannot compute the square root. So if
a
is supplied, it will return a result, but if b
is supplied, nothing
happens.
The reason is that the multiplier has three total connectors, two of which need
to have a value for the third one to be calculated. If we supply a
, this
condition will be fulfilled, since a
goes to both input connectors. But if we
supply only b
, only one connector will actually have a value, which will not
be sufficient for the multiplier constraint to determine the value of the two
remaining ones. Or, in other words, there is no way for the multiplier to know
that two of its inputs are always going to be exactly the same, and that they
therefore don’t need to both be set.