CS61A_fa94_mt2_sol - CS 61A Fall 1994 Midterm 2 solutions...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 61A Fall 1994 Midterm 2 solutions 1. Box & pointer diagrams > (cons (append '(a b) '(c d)) '(e f)) ((A B C D) E F) Many people said ((a b c d) . (e f)) but Scheme never prints the sequence ". (" in representing a list structure. In the picture below, the starred pair is the one created by the CONS invocation. *********** *+---+---+* +---+---+ +---+---+ *| | |* | | | | | /|--------->*| | | ----------->| | | ----->| | | / | *| | | |* | | | | | | |/ | *+-|-+---+* +-|-+---+ +-|-+---+ ***|******* | | | V V | | E F | V +---+---+ +---+---+ +---+---+ +---+---+ | | | | | | | | | | | /| | | | ----->| | | ----->| | | ----->| | | / | | | | | | | | | | | | | | | |/ | +-|-+---+ +-|-+---+ +-|-+---+ +-|-+---+ | | | | V V V V A B C D > (list (cons 'a 'b) (list 'c 'd) 'e) ((A . B) (C D) E) +---+---+ +---+---+ +---+---+ | | | | | | | | /|---------> | | | ----->| | | ----->| | | / | | | | | | | | | | | |/ | +-|-+---+ +-|-+---+ +-|-+---+ | | | | | V | | | | E V V +---+---+ +---+---+ +---+---+ | | | | | | | | /| | | | | | | | | ----->| | | / | | | | | | | | | | | | |/ | +-|-+-|-+ +-|-+---+ +-|-+---+ | | | | V V V V A B C D > (caaddr '((a b c d e) (f g h i j) (l m n o p) (q r s t u))) L (cdr '((a b c d e) (f g h i j) (l m n o p) (q r s t u))) ===> ((f g h i j) (l m n o p) (q r s t u)) (cdr '((f g h i j) (l m n o p) (q r s t u))) ===> ((l m n o p) (q r s t u)) (car '((l m n o p) (q r s t u))) ===> (l m n o p) (car '(l m n o p)) ===> L The box and pointer diagram for this is just-----> L We accepted "you can't have a box and pointer diagram with no pairs" and we accepted a single box around the L, but we didn't accept anything with a pair above the L. > (cons 'a (cdr '(((b))))) (A) (cdr '(((b)))) is the empty list, so this is equivalent to (cons 'a '())....
View Full 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 / 5

CS61A_fa94_mt2_sol - CS 61A Fall 1994 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