{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS61A_sp95_mt2_sol

CS61A_sp95_mt2_sol - CS61A Spring 1995 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 61A Spring 1995 Midterm 2 solutions 1. Box & pointer diagrams > (cddaar '(((a b c d e) (f g h i j)) ((l m n o p) (q r s t u)))) (C D E) (car '(((a b c d e) (f g h i j)) ((l m n o p) (q r s t u)))) ===> ((a b c d e) (f g h i j)) (car '((a b c d e) (f g h i j))) ===> (a b c d e) (cdr '(a b c d e)) ===> (b c d e) (cdr '(b c d e)) ===> (c d e) The box and pointer diagram for this is just +---+---+ +---+---+ +---+---+ | | | | | | | | /| ---------> | | | ----->| | | ----->| | | / | | | | | | | | | | | |/ | +-|-+---+ +-|-+---+ +-|-+---+ | | | V V V C D E > (cons '(a b) (append '(c d) '(e f))) ((A B) C D E F) Some 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 V V | | C D E F CS61A: Spring 1995 Midterm #2 solutions 1
Image of page 1

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

View Full Document Right Arrow Icon
| V +---+---+ +---+---+ | | | | | /| | | | ----->| | | / | | | | | | | |/ | +-|-+---+ +-|-+---+ | | V V A B Scoring: one-half point for each printed result, one-half point for each box & pointer diagram, rounded down. 2. Type of last element of each result. > '(list (count 'hello)) (LIST (COUNT 'HELLO)) So the last element is the (sub)list (COUNT 'HELLO). --> LIST > '(list count) (LIST COUNT) So the last element is the word COUNT. --> NON-NUMERIC WORD > (list 'count count) (COUNT #) So the last element is the procedure named count. --> PROCEDURE > (list 'hello (count 'hello)) (HELLO 5) So the last element is the number 5. --> NUMBER Scoring: One half point each, rounded down. 3. Tree-accumulate. Here is the solution we were expecting: (define (tree-accumulate fn tree) CS61A: Spring 1995 Midterm #2 solutions 2
Image of page 2
(if (null? (children tree)) (datum tree) (fn (datum tree) (forest-accumulate fn (children tree))))) (define (forest-accumulate fn forest) (if (null? (cdr forest)) (tree-accumulate fn (car forest)) (fn (tree-accumulate fn (car forest)) (forest-accumulate fn (cdr forest))))) Alternatively, it can be done using other higher-order functions, especially if you learned about REDUCE in CS 3: (define (tree-accumulate fn tree) (if (null? (children tree)) (datum tree) (fn (datum tree) (reduce fn (map (lambda (t) (tree-accumulate fn t)) (children tree)))))) REDUCE takes a function and a list (a sequence, not a tree!) and accumulates (fn element-1 (fn element-2 (... (fn element-n-1 element-n)...))) Forest-accumulate has a slightly messier than usual base case, because there must be at least one tree in the forest in order to get an answer. (You can't assume that the identity element for FN is zero; that's true for +, and you might think it's true for MAX if you forget about negative numbers, but zero isn't the identity for * (1), SENTENCE ('()), WORD (""), or lots of other possible functions. But we only took off one point for any base-case confusion. *** RESPECTING THE DATA ABSTRACTION DOES **NOT** MEAN SAYING "DATUM" WHENEVER *** YOU MEAN CAR, AND "CHILDREN" WHENEVER YOU MEAN CDR!! That is precisely DISrespecting the data abstraction! Respecting it means to distinguish between a tree, whose component parts are called datum and children, and other data types that have other component parts, such as forests (car and cdr, since
Image of page 3

Info icon This 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