This preview shows pages 1–3. Sign up to view the full content.
61A
Fall 2009
Midterm 2 solutions
1.
What will Scheme print?
> (let ((theprofessor bh))
(ask theprofessor 'greet))
(HI MY NAME IS BRIAN)
The main point of this problem was to make sure you understand
that more than one variable name can refer to the same object,
but in fact the most common error was to leave out the parentheses.
> (greet bh)
ERROR
Most people got this: a method defined inside an object class isn't
a global variable (or a variable of any kind, in fact), so there is
no procedure named GREET.
Scoring:
One point each.
2.
Box and pointer diagram
> (define x '(1 2))
> (list [cons (append x '(5)) x])
(((1 2 5) 1 2))
It's important not to make assumptions about which parenthesis
matches which in this problem!
I've replaced one matching pair
of parentheses with [square brackets] above to make it easier
to see that the call to LIST has only one argument (the entire
expression in square brackets), so it returns a list of one
element.
That element is itself a list, which is why there are
two pairs of parentheses around the entire result: one for the
inner list and one for the singleelement list made by LIST.
Here's the diagram:
>X/
x


V
V
**>XX>X/




V
V

1
2

V
XX>XX>X/



V
V
V
1
2
5
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
(CONS always makes exactly one pair.)
Its CAR, the result of
the APPEND call, is the bottom row of three pairs.
Its CDR is
the original list X.
(The diagram above shows which pair is the
value of the variable X, but you weren't required to do that.)
The most common error was to leave out the topmost pair, the one
made by the LIST call.
Scoring:
One point for the printed result, one for the diagram.
3.
EVAL and APPLY in the calculator program
As I explained in lecture, the importance of the calculator example lies
in
the fact that interpreters for real programming languages, including
Scheme,
revolve around procedures EVAL and APPLY that work much like (but not
exactly
like, as we'll see in chapter 4) the ones here.
CALCEVAL takes one argument, an /expression/, and returns the /value/ of
that expression.
So its domain is expressions.
A typical call would be
(calceval '(+ (* 5 4) ( 9 3)))
which (to save you time in writing your answer) we abbreviated as
E (+ (* 5 4) ( 9 3))
CALCAPPLY takes two arguments: an arithmetic operation symbol (+, , *,
/)
and a list of actual argument /values/.
EVAL works in the world of
expressions  things typed in by users  but APPLY works in the world
of
values, after all the notational issues have been handled by EVAL.
Thus,
the call to CALCAPPLY that finally determines the value of the
expression
above will be
(calcapply '+ '(20 6))
which you would have abbreviated as
A + (20 6)
But that call to CALCAPPLY can't happen until we have argument /values/
to
give it, which means that all of the evaluation of argument
subexpressions
happens before it.
This is another way of saying that the calculator, like Scheme, uses
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 11/23/2010 for the course CS 61A taught by Professor Harvey during the Fall '08 term at University of California, Berkeley.
 Fall '08
 Harvey
 Computer Programming

Click to edit the document details