CS61A_sp01_mt1_sol - CS 61A Spring 2001 Midterm #1...

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

View Full Document Right Arrow Icon
CS 61A Spring 2001 Midterm #1 solutions 1. What will Scheme print? > (butfirst (butlast (se '(this) 'is '(easy)))) (IS) (se '(this) 'is '(easy)) ==> (this is easy) (butlast '(this is easy)) ==> (this is) (butfirst '(this is)) ==> (is) The last step was the hardest; some people said IS rather than (IS). But there's a big difference between a word and a one-word sentence: (first 'is) ==> I (first '(is)) ==> IS Butfirst of a sentence is always a sentence, never a word. > (car ((lambda (lst) (cdr lst)) '((1 2) (3 4)))) (3 4) The easiest way to think about this is to see that (lambda (lst) (cdr lst)) is the same function as CDR, so this expression is really just (car (cdr '((1 2) (3 4)))) (cdr '((1 2) (3 4))) ==> ((3 4)) (car '((3 4))) ==> (3 4) Analagously to the first problem, CDR of a two-element list is a one-element sublist, *not* just the second element. CADR gives the second element. > (let ((rotate (lambda (a b c) (if (number? a) a (b c 2 3))))) (rotate rotate rotate 1)) 1 Substituting ROTATE for A, ROTATE for B, and 1 for C in the body of ROTATE gives (if (number? rotate) rotate (rotate 1 2 3)) ROTATE isn't a number; it's a procedure. So we call ROTATE again, this time with A=1, B=2, C=3: (if (number? 1) 1 (2 3 2 3)) This time, 1 is a number, so we return 1. > (if (equal? '2 2) + -) PROCEDURE Many people found this one tricky, answering either + or - rather than PROCEDURE. In fact (EQUAL? '2 2) returns #T, so Scheme *evaluates* the expression +. But its value isn't +, but is the primitive addition procedure.
Background image of page 1

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

View Full DocumentRight Arrow Icon
> (map butfirst '((she loves you) (help!) (penny lane))) ((LOVES YOU) () (LANE)) The common wrong answer here was "error," but there's nothing wrong with this expression, not even a data abstraction violation. The second argument to MAP isn't a sentence, so it isn't a valid argument to BUTFIRST. But MAP doesn't call BUTFIRST with that whole thing as its argument! Rather, MAP calls BUTFIRST three times: (butfirst '(she loves you)) ==> (loves you) (butfirst '(help!)) ==> () (butfirst '(penny lane)) ==> (lane) MAP makes a list of these three results. The second argument to MAP is a *list of sentences*, so each element is a sentence, so it *is* a valid argument to BUTFIRST. Another common wrong answer was ((LOVES YOU) (ELP!) (LANE)). This comes from wanting to treat a one-word sentence as if it were a word. But words and sentences are not the same data type. BUTFIRST of an N-word sentence is always an (N-1)-word sentence, even if N=1. Scoring: 1 point each. 2. Every-nth and data abstraction The key point here is the same as in the last part of question 1: We are dealing with a *list of sentences*, so the list itself should be examined using CAR/CDR/NULL?, but each element of that list is a sentence, so it should be examined using FIRST/BF/EMPTY?. The return value from EVERY-NTH is supposed to be a *sentence*, so we should construct it using SENTENCE: (define (every-nth num list-of-sents) (define (nth num sent) (if (= num 0) (FIRST sent) (nth (- num 1) (BUTFIRST sent))))
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 / 8

CS61A_sp01_mt1_sol - CS 61A Spring 2001 Midterm #1...

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