CS61A_sp92_mt2_sol

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

Info icon This 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. Box & pointer diagrams (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: +---+---+ +---+---+ | | | | | /| ---------> | | | ----->| | | / | | | | | | | |/ | +-|-+---+ +-|-+---+ | |
Image of page 1

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

View Full Document Right 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: +---+---+ +---+---+
Image of page 2
| | | | | /| ---------> | | | ----->| | | / | | | | | | | |/ | +-|-+---+ +-|-+---+ | | | V | | a | V +---+---+ +---+---+ | | | | | /| | | | ----->| | | / | | | | | | | |/ | +-|-+---+ +-|-+---+ | | | | V V 2 3 Scoring: One point each; you had to get both the printed result and the diagram to get the point. Also, if you would have had four points but you left out all the arrows pointing to the heads of the lists, we took off a point. 2. uniq Is this sequential or tree recursion? We are looking at each element of the list as one whole thing, not delving into the structure of sublists, so it's sequential recursion.
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern