Computer Science 61A - Spring 2006 - Harvey - Midterm 2 Solutions

Computer Science 61A - Spring 2006 - Harvey - Midterm 2 Solutions

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

View Full Document Right Arrow Icon
http://www-inst.eecs.berkeley.edu/~cs61a/sp06/solutions/mt2 1 of 5 5/11/2006 12:10 AM CS 61A Spring 2006 Midterm 2 solutions 1. Box and pointer. Note: Please draw actual boxes, as in the book and the lectures, not XX and X/ as in these ASCII-art solutions. Also, please put in the start arrows! Sometimes it's hard to know where your diagrams are supposed to begin, especially if you use leftward and upward pointing arrows. > (cons (list '(a) '(b)) (append '(c) '(d))) (((a) (b)) c d) --->XX--->**--->X/ | | | | V V | c d | V XX--->X/ | | V V X/ X/ | | V V a b The pair marked ** in the diagram above is the value of the subexpression (APPEND '(C) '(D)), namely, the list (C D). The CONS sticks one pair in front of that, the pair at the start arrow. Its car, the first element of the new list, is the value of (LIST '(A) '(B)), which is ((A) (B)). > (car (cons '((portia)) '(black . satin))) ((PORTIA)) --->X/ | X/ | V portia This should have been really easy, because CAR is an inverse function of CONS; that is, (CAR (CONS X Y)) is always X, whatever X might be. So you could entirely ignore the second argument to CONS. The double parentheses in ((PORTIA)) mean that it's a list of one element, which is itself a list of one element, a word. > (caadr '( ((a b c) (d e f)) ((g h i) (j k l)) ((m n o) (p q r)) )) (g h i) --->XX--->XX--->X/ | | | V V V g h i This was mainly a test of whether you understand the order of operations in the C. ..R functions. CAADR means the CAR of the CAR of the CDR, not "first do CAR then CAR then CDR"! So we have (cdr '(((a b c) (d e f)) ((g h i) (j k l)) ((m n o) (p q r)))) (car '(((g h i) (j k l)) ((m n o) (p q r)))) (car '((g h i) (j k l))) (g h i) A shortcut to this solution is to remember that CADR returns the second element of a list, so you can get to ((G H I) (J K L)) in one step, then take the CAR (the first element) of that.
Background image of page 1

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

View Full DocumentRight Arrow Icon
http://www-inst.eecs.berkeley.edu/~cs61a/sp06/solutions/mt2 2 of 5 5/11/2006 12:10 AM If you did the operations in reverse order, you got the wrong answer (B C). > (filter (lambda (x) (if (list? x) (pair? x) (number? x))) '(1 () (2 3) (so) what)) (1 (2 3) (so)) --->XX--->XX-------->X/ | | | V V V 1 XX--->X/ X/ | | | V V V 2 3 so This was an exercise in understanding FILTER, IF, and predicates. The IF subexpression returns true for a list that's also a pair (i.e., not the empty list) or for a non-list that's a number (not a non-numeric word, for example). So these tests are made: element first test result second test result 1 LIST? #F NUMBER? #T () list? #t pair? #f (2 3) LIST? #T PAIR? #T (SO) LIST? #T PAIR? #T what list? #f number? #f The lines in capital letters show the elements that FILTER keeps. To solve this problem correctly you also had to understand that
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 04/01/2008 for the course CS 61A taught by Professor Harvey during the Spring '08 term at University of California, Berkeley.

Page1 / 5

Computer Science 61A - Spring 2006 - Harvey - Midterm 2 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