CS61A_fa00_mt1_sol - CS 61A Fall 2000 Midterm #1 solutions...

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

View Full Document Right Arrow Icon
CS 61A Fall 2000 Midterm #1 solutions 1. What will Scheme print? (let ((a 3) (b 4)) (lambda () (+ a b))) PROCEDURE The body of the LET is (lambda () (+ a b)). The value of that expression is a procedure -- after substitution, it's the same procedure as (lambda () 7). But it's not the number 7, which was the most common wrong answer. If we named this procedure P, then the expression (P) would mean to invoke P, and *that* would have the value 7. (let ((a 3) (b 4)) ((lambda () (* a b)))) 12 It's 12, not 7, because this one uses *, not +. But the important thing is that those extra parentheses invoke the procedure, just like the (P) discussion above. So the value isn't a procedure this time. Some people said 7 for the first example and procedure for this one; we haven't figured out what they were thinking. (every - (filter number? '(the 1 after 909))) (-1 -909) The FILTER part of the expression has the value (1 909), selecting the numbers from the given sentence. (By the way, not everyone recognized this as the name of a Beatles song, from the album _Let It Be_, the last one they released, but the next-to-last one they recorded.) A few people thought this was a data abstraction violation, because FILTER returns a list, not a sentence, and EVERY definitely wants a sentence as argument. I guess this could be confusing, but I defined in lecture during week 2 a FILTER that works on sentences, and that's the one we meant here. (every - '(1 909)) computes (- 1) and (- 909), putting the results in a sentence. Some people thought this was an error because - requires two arguments, but it doesn't. With one argument it means negation, rather than subtraction. (Look it up in the Scheme reference manual at the back of your course reader!) A few people said -908, because they thought EVERY would call the function once, giving both numbers as arguments. That would be (APPLY - '(1 909)), not EVERY. Scoring for the above: One point each, all or nothing. (cons '(a b) (list '(c d) 'e)) result =======>XX----------------------+ | | | |
Background image of page 1

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

View Full DocumentRight Arrow Icon
V V XX-->X/ list===>XX-->X/ | | | | | | | | V V | V a b | e | V XX-->X/ | | | | V V c d In the diagram above I've indicated the result of the LIST subexpression. The overall result shows, on the first line of the diagram, the one pair created by the CONS call. Its car is (a b) and its cdr is the result from the LIST subexpression, which created two pairs. How does this print? The pair created by the CONS has a list as its cdr, so it's a list, containing three elements. The first element is (a b), the second is (c d), and the third is e: ((a b) (c d) e) The most common mistake was to represent (a b) as a single pair, as if it were (a . b). Another common mistake was to put an extra pair where the + sign is in the top line of the diagram, as if the CONS call were a LIST call. Please put a start arrow showing the pair at the head of your
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 / 9

CS61A_fa00_mt1_sol - CS 61A Fall 2000 Midterm #1 solutions...

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