CS61A_fa98_mt1_sol - CS61a Midterm#1 Fall 1998-> CS 61A 0...

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

View Full Document Right Arrow Icon
CS 61A Fall 1998 Midterm #1 solutions 0. Name, etc. Some people did not read and/or sign the box on the front that said READ AND SIGN THIS in big bold letters. Other than that, I was pleased that this semester's students did better than usual at knowing which sections you're in. 1. What will Scheme print? > (3 + 5) ERROR Almost everyone remembered that in Scheme the procedure comes first, not between the arguments. This expression is attempting to call the number 3 as if it were a procedure. > (butfirst (first (butfirst '(i want to tell you)))) ANT (bf '(i want to tell you)) --> (want to tell you) (first '(want to tell you)) --> want (bf 'want) --> ant One common wrong answer was (ANT) -- a sentence containing the word ANT. Words and sentences are two different kinds of data. Another wrong answer was 'ANT -- the correct word with a quotation mark in front of it. I think you think that the quotation mark means "this is a word" or some such thing; but it really means "don't evaluate this expression." You type quotation marks in to Scheme, not the other way around. Finally, many people said () -- the empty sentence. I'm not sure why; perhaps they thought that (first '(want to tell you)) --> (want) [WRONG] for which the butfirst would indeed be empty. > ((lambda (a b c) (b a c)) 3 + 5) 8 Several people thought this was an error. Perhaps they were confused because the body of the procedure, (B A C), looks like the formal parameter list, (A B C), and so they thought CS61a , Midterm #1, Fall 1998) --> course, semester/yearexam #professor (e.g., Professor J. Wawrzynek) 1
Background image of page 1

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

View Full Document Right Arrow Icon
that 3 + 5 was the body. But actually 3, +, and 5 are the actual argument expressions corresponding to the formal parameters A, B, and C respectively. So, substituting the arguments for the parameters in the body (B A C) gives (+ 3 5) and the value of that expression is 8. A few people said this returned a procedure. But the lambda expression is enclosed within a larger expression -- there are two sets of parentheses around it -- and so the procedure that lambda creates is then invoked, not returned. > (let ((a +) (b (a 3 4))) (a b b)) ERROR The main point here is that the expression (a 3 4), which is supposed to provide a value for B, is evaluated *outside* the scope of the LET, so that A is *not* bound to the + procedure in that scope. The error is that A is unbound during the attempt to evaluate (a 3 4). The most common wrong answer was 14, which is what would be the result if B were bound to 7 during the evaluation of (A B B). This answer *would* be correct if the LET were a LET* instead. Then the expression (a 3 4) would be evaluated with A bound to the + procedure. > (cond ((<4 7) 5) ((> 4 2) 6) (else 3)) 5 ( <4 7) is true, so COND returns 5 and never looks at the other clauses at all. Most people got this, but one interesting wrong answer was 5 6 as if COND could return two values. (I'm guessing this person thinks that COND *prints* things, in which case it could certainly print more than one thing -- but there's no printing
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.

{[ snackBarMessage ]}

Page1 / 9

CS61A_fa98_mt1_sol - CS61a Midterm#1 Fall 1998-> CS 61A 0...

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