Solved exercises in Scheme (Lisp) from 'Structure and Interpretation of Computer Programs' (MIT Press)
Below is a sequence of expressions. What is the result printed by the interpreter in response to each expression? Assume that the sequence is to be evaluated in the order in which it is presented.
10
12
8
3
6
19
#f
4
16
6
16
Translate the following expression into prefix form
Evaluating the expression yields -37/150
. This is because the enumerator evaluates to 74/5
and the denominator to -60
. All numerals are specified as integer as if (/ (- 37) 150)
had been given. In contrast, the expression (/ (- 37.0) 150)
evaluates to 0.24666666666666667
.
Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.
Prerequisites—as in the book
A solution
Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure:
The operator is a compound expression that evaluates to either ‘+’ or ‘-‘ depending on the sign of ‘b’, i.e. whether or , respectively.
Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluation or normal-order evaluation. He defines the following two procedures:
Then he evaluates the expression
What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normal-order evaluation? Explain your answer. (Assume that the evaluation rule for the special form if is the same whether the interpreter is using normal or applicative order: The predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.)
Evaluating (p)
starts an infinitely recurring process since procedure p
has been defined
recursively without any base case(s). So, under applicative order the evaluation
of the expression (test 0 (p))
is never completed because both arguments of
test
—which include (p)
—must be evaluated before evaluation of the body of test
. In
contrast, under normal order the argument (p)
would only be evaluated if the
(= x 0)
predicate were false. However, this is not the case, so the evaluation of
(test 0 (p))
is completed immediately after the 0
consequent is evaluated. Thus, Ben Bitdiddle’s reasoning is correct, (test 0 (p))
does indeed helps distinguish between applicative and normal order evaluation.
Alyssa P. Hacker doesn’t see why if needs to be provided as a special form. “Why can’t I just define it as an ordinary procedure in terms of cond?” she asks. Alyssa’s friend Eva Lu Ator claims this can indeed be done, and she defines a new version of if:
Eva demonstrates the program for Alyssa:
Delighted, Alyssa uses new-if to rewrite the square-root program:
What happens when Alyssa attempts to use this to compute square roots? Explain.
Infinitely recurring process is started for the same reason as in the case of
Ben Bitdiddle’s (test 0 (p))
(Exercise 1.5). So, the shortcoming of new-if
is that it is a procedure and hence—under applicative order of
evaluation—all of its arguments must be evaluated first even when the
evaluation of some of those arguments never completes.
The
good-enough?
test used in computing square roots will not be very effective for finding the square roots of very small numbers. Also, in real computers, arithmetic operations are almost always performed with limited precision. This makes our test inadequate for very large numbers. Explain these statements, with examples showing how the test fails for small and large numbers. An alternative strategy for implementinggood-enough?
is to watch howguess
changes from one iteration to the next and to stop when the change is a very small fraction of the guess. Design a square-root procedure that uses this kind of end test. Does this work better for small and large numbers?
The original good-enough?
Tests for later comparison with a very small and large number as argument:
Clearly, the result is very inaccurate for the very small argument.
The following procedure may be expected to correctly implement the alternative strategy described above in prose.
But for reasons unclear at this moment the new implementation performs worse than the original one:
Newton’s method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better approximation is given by the value
Use this formula to implement a cube-root procedure analogous to the square-root procedure. (In section 1.3.4 we will see how to implement Newton’s method in general as an abstraction of these square-root and cube-root procedures.)
A set of possible procedures:
Note that good-enough-cube?
is similar to new good-enough?
(Exercise 1.7) which seems to perform worse than the old good-enough?
.
’’ ‘’