CS61A_sp95_mt2_sol - CS61A: Spring 1995 Midterm #2...

Info iconThis 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. > (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
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 V A B Scoring: one-half point for each printed result, one-half point for 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
Background 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
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 University of California, Berkeley.

Page1 / 8

CS61A_sp95_mt2_sol - CS61A: Spring 1995 Midterm #2...

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