CS61A_sp92_mt2_sol

CS61A_sp92_mt2_sol - CS 60A Spring 1992 Midterm 2 solutions...

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

View Full Document Right Arrow Icon
CS 60A Spring 1992 Midterm 2 solutions 1. (cons (cdr '(x)) 3) (cdr '(x)) is the empty list, so this is the same as (cons '() 3) and therefore the result is: (() . 3) There is a dot in the printed result because it is not a proper list; it contains a pair (its only pair, in fact) whose cdr is neither a pair nor the empty list. Diagram: +---+---+ | /| | ---------> | / | | | |/ | | | +---+-|-+ | | V 3 It's okay if the left side of the box has a pointer to a SINGLE box with a slash (representing the empty list) or a pointer to (), but it's wrong if the left side of the box points to a PAIR with two slashes. That would be a list containing an empty list, (()). It's also wrong if you said that Scheme would print (nil . 3); the symbol "nil" happens to be a name for the empty list, as if we'd said (define nil '()), but it's not the empty list itself. If this is confusing, think about what would happen if you said (define a 3) and then said (+ 1 2). You wouldn't expect Scheme to print "a," would you? (Yes, this is different in Common Lisp, where "nil" is not an ordinary symbol but a special magic cookie.) (list 2 '(3 4)) LIST creates a list containing its arguments as members. Here we are calling it with two arguments, so we end up with a list of two elements, one of which is itself a list: (2 (3 4)) No improper lists here, so no dots. Diagram: +---+---+ +---+---+ | | | | | /| ---------> | | | ----->| | | / | | | | | | | |/ | +-|-+---+ +-|-+---+ | |
Background image of page 1

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

View Full DocumentRight Arrow Icon
| | V V +---+---+ +---+---+ 2 | | | | | /| | | | ----->| | | / | | | | | | | |/ | +-|-+---+ +-|-+---+ | | | | V V 3 4 ((lambda (s) (cons (car s) (cddr s))) '(w x y z)) If "s" represents the list (w x y z), then (car s) is w and (cddr s) is (y z). In effect we are doing (cons 'w '(y z)), creating a three-element list. (Note that CONS and LIST do different things! CONS always creates exactly one new pair; LIST creates as many new pairs as it has arguments.) So this prints (w y z) Diagram: +---+---+ +---+---+ +---+---+ | | | | | | | | /| ---------> | | | ----->| | | ----->| | | / | | | | | | | | | | | |/ | +-|-+---+ +-|-+---+ +-|-+---+ | | | | | | V V V w y z The worst mistake here was that some people had the word S in their results! "S" is a formal parameter of the function we're invoking; the actual argument (w x y z) is substituted for it when we call the function! (cons (list 2 3) '(a)) CONS always creates one new pair. Its first argument is a list that has two pairs in it; the second argument is a list with one pair. We'll end up with four pairs altogether. When the second argument to CONS is a list, the result is also a list, with one new element at the front. So there won't be any dots in the printed form: ((2 3) a) Note that it isn't ((2 3) (a)), which is what you'd get if you used LIST instead of CONS. Diagram: +---+---+ +---+---+
Background image of page 2
| | | | | /| ---------> | | | ----->| | | / | | | | | | | |/ | +-|-+---+ +-|-+---+ | | | V | | a | V +---+---+ +---+---+ | | | | | /| | | | ----->| | | / | | | | | | | |/ | +-|-+---+
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
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 Berkeley.

Page1 / 7

CS61A_sp92_mt2_sol - CS 60A Spring 1992 Midterm 2 solutions...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online