Cs61a_fa09_mt2_sol - 61A 1 Fall 2009 What will Scheme print Midterm 2 solutions >(let(the-professor bh(ask the-professor'greet(HI MY NAME IS BRIAN

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

View Full Document Right Arrow Icon
61A Fall 2009 Midterm 2 solutions 1. What will Scheme print? > (let ((the-professor bh)) (ask the-professor '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 single-element 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
(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. CALC-EVAL takes one argument, an /expression/, and returns the /value/ of that expression. So its domain is expressions. A typical call would be (calc-eval '(+ (* 5 4) (- 9 3))) which (to save you time in writing your answer) we abbreviated as E (+ (* 5 4) (- 9 3)) CALC-APPLY 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 CALC-APPLY that finally determines the value of the expression above will be (calc-apply '+ '(20 6)) which you would have abbreviated as A + (20 6) But that call to CALC-APPLY 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
Background image of page 2
Image of page 3
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.

Page1 / 10

Cs61a_fa09_mt2_sol - 61A 1 Fall 2009 What will Scheme print Midterm 2 solutions >(let(the-professor bh(ask the-professor'greet(HI MY NAME IS BRIAN

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

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