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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
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 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_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 Right Arrow Icon
Ask a homework question - tutors are online