{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS61A_sp94_mt2_sol

CS61A_sp94_mt2_sol - CS 61A Spring 1994 Midterm 2 solutions...

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

CS 61A Spring 1994 Midterm 2 solutions 1. > (list '(2 3) '(4 5)) ((2 3) (4 5)) +---+---+ +---+---+ | | | | | /| ---------> | | | --------------------------->| | | / | | | | | | | |/ | +-|-+---+ +-|-+---+ | | | | V V +---+---+ +---+---+ +---+---+ +---+---+ | | | | | /| | | | | | /| | | | ----->| | | / | | | | ----->| | | / | | | | | | | |/ | | | | | | | |/ | +-|-+---+ +-|-+---+ +-|-+---+ +-|-+---+ | | | | | | | | | | | | V V V V 2 3 4 5 The LIST constructor makes a list whose elements are its arguments. In this case, that means a list of two elements, whose spine is the two pairs on the top row of the diagram. > cons (list 2 3) 4) ((2 3) . 4) +---+---+ | | | ---------> | | | --------> 4 | | | | +-|-+---+ | | | | | V +---+---+ +---+---+ | | | | | /| | | | ----->| | | / | | | | | | | |/ | +-|-+---+ +-|-+---+ | | | | V V 2 3 CONS always constructs exactly one pair -- in this case, the one on the

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

View Full Document
top row of the diagram. Because the second argument to CONS isn't a list, the return value isn't a list either, so there's a dot in its printed representation. > (cddadr '((a b c d e) (f g h i j) (l m n o p) (q r s t u))) (H I J) +---+---+ +---+---+ +---+---+ | | | | | | | | /| ---------> | | | ----->| | | ----->| | | / | | | | | | | | | | | |/ | +-|-+---+ +-|-+---+ +-|-+---+ | | | | | | V V V H I J (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))) (car '((f g h i j) (l m n o p) (q r s t u))) ===> (f g h i j) (cdr '(f g h i j)) ===> (g h i j) (cdr '(g h i j)) ===> (h i j) > (cons (cdr '(a)) (cdr '(b))) (()) +---+---+ | /| /| ---------> | / | / | |/ |/ | +---+---+ This seems to have been the hardest for most people. The printed form can't be (() . ()), because Scheme *never* prints a dot if the next thing would be a left parenthesis. If in your diagram you drew an arrow pointing to () we accepted it, but if your arrow pointed to '() we didn't, since that means you don't understand what the quote means. There is only one pair in this diagram! Scoring: One point each. 2.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 5

CS61A_sp94_mt2_sol - CS 61A Spring 1994 Midterm 2 solutions...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online