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

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

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

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.

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

View Full Document
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 there is no recursion here, so this is not a deep-list problem: the elements of the elements are not tests (which would have rejected the word SO).
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