{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS61A_sp01_mt1_sol

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

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

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.

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

View Full Document
> (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)))) (if (NULL? list-of-sents) '() (SENTENCE (nth num (CAR list-of-sents)) (every-nth num (CDR list-of-sents))))) The formal parameters were chosen to be helpful; there wasn't anything tricky about this problem.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern