2011-09-14

SICP Exercise 1.34: Self Reference

Suppose we define the procedure
(define (f g)
  (g 2))
Then we have
> (f square)
4
> (f (lambda (z) (* z (+ z 1))))
6
What happens if we (perversely) ask the interpreter to evaluate the combination (f f)? Explain.

If we ask the interpreter to evaluate (f f) then we'll get an error. We can work this though with substitution:
  (f f)
= (f 2)
= (2 2)
As you can see, evaluating (f f) leads the interpreter to try to apply the integer 2 as an operator, which it isn't.

Let's just confirm that. I happen to be having a quick look at SISC tonight. Here's what happens when we ask it to evaluate (2 2):
#;> (2 2)
Error: attempt to apply non-procedure '2'.

No comments:

Post a Comment