SICP Exercise 1.34
Question
Suppose we define the procedure
(define (f g)
(g 2))
Then we have
(f square)
4
and
(f (λ (z) (* z (+ z 1))))
6
What happens if we (perversely) ask the interpreter to evaluate the combination (f f)
?
Explain.
Answer
The easiest way to explain this is to try running the function:
(define (f g)
(g 2))
(f f)
We receive an error message:
application: not a procedure;
expected a procedure that can be applied to arguments
given: 2
Let us try to understand why this happens by stepping through (f f)
.
The function f
could be translated into English as “apply the following function to 2”, or “call the following function with the parameter 2”.
So if we call (f f)
, we are basically saying “call the function f
with the parameter 2
", which is the same thing as (f 2)
.
So, for the next step, we need to evaluate (f 2)
.
(f 2)
could be translated as “apply the function 2
to the value 2
", or (2 2)
.
The problem is that, of course, 2
is not a valid function.
Therefore, we receive the above error message.