notes07s - 1

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
You Are Scheme – and don’t let anyone tell you otherwise QUESTIONS 1. If I type this into STK, I get an unbound variable error: (eval-1 ‘x) This surprises me a bit, since I expected  eval-1  to return  x , 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 error. 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: (eval-1 ‘‘x) Note that if you type just ‘x into Scheme1, it works: Scheme1: ‘x x 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 previous case). 2. Hacking Scheme1: For some reason, the following expression works: (‘(lambda(x) (* x x)) 3) Note the quote in front of the  lambda  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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

notes07s - 1

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online