You Are Scheme – and don’t let anyone tell you otherwise
If I type this into STK, I get an unbound variable error:
This surprises me a bit, since I expected
, unquoted. Why did this happen?
What should I have typed in instead?
The quote in front of x protects x from STk, but not from Scheme1.
Recall that eval-1 is just a procedure, and for STk to make that
procedure call, it first evaluates all its arguments – including (quote
x) – before passing the argument to eval-1.
Then, when eval-1 sees the
symbol x, it tries to cal eval on it, throwing an unbound variable
The problem, then, is that the expression ‘x is evaluated TWICE – once
by the STk evaluator, and once by eval-1.
Thus, to protect it twice,
you need two quotes:
Note that if you type just ‘x into Scheme1, it works:
Make sure you understand the difference, and why you don’t need double-
quote there (because STk is never told to evaluate ‘x, unlike the
Hacking Scheme1: For some reason, the following expression works:
(‘(lambda(x) (* x x)) 3)
Note the quote in front of the
expression. Well, it’s not supposed to! Why does it work?
What fact about Scheme1 does this exploit?
When eval-1 sees the procedure call and tries to evaluate the
procedure, it sees that it is a quoted expression, and unquotes it.
Then, the procedure is passed to apply-1, which sees that it is a
lambda expression, and uses it as such.
This exploits the fact that in
Scheme1, a compound procedure is represented as a list that looks
exactly like the lambda expression that created it.
Thus, even though
we never evaluated the lambda expression into a procedure value,
Scheme1 is still fooled into thinking it’s a valid procedure.
Chung Wu; CS61A, Spring 2004