CS61A_fa02_mt2

# CS61A_fa02_mt2 - CS 61A, Fall, 2002, Midterm #2, L. Rowe 1....

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

CS 61A, Fall, 2002, Midterm #2, L. Rowe 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams. For each of the following Scheme expressions, indicate which diagram is produced when the expression is evaluated. It may be that some diagrams above are not used in any answer below, and the diagrams for some answers may appear above. Enter the answer “none” if the result of evaluating the expression does not match a diagram above. (i) (list (cons 1 2) (cons ‘() 3)) (ii) (list (append 1 2 ‘(3))) (iii) ‘((1 2 (3))) (iv) (accumulate cons nil (filter number? ‘(a 1 b 3 c d 3))) (v) (list 1 2 ‘(3)) (vi) (let ((x ‘(1))) (set-cdr! x (cons 2 3))) 1 2 3 a) d) 1 2 3 b) 1 2 3 3 1 2 e) c) 1 2 3

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

View Full Document
(vii) (append (list ‘(1 2)) ‘(3)) (viii) (let ((x (list 1 2 3))) (set-car! (cddr x) (cons (caddr x) nil)) (list x)) (ix) ‘((1 2) 3) (x) ‘(1 (2) 3) 2. (10 points) The textbook suggests that we can view lists that contain lists as trees. For example, the list ‘(1 (2 3) 4) can be thought of as a tree that looks like. As you can see, a branch of a tree connects two nodes. A node with no branches is called a leaf node . This tree has six nodes and five branches. Four nodes are leaves, one node is the root, and the remaining node has two leaves (i.e., 2 and 3) below it. (i) (4 points) Draw a box-and-arrow diagram for the example list ‘(1 (2 3) 4). Remember to add the arrow that points at the list. (ii) (2 points) Eva Lu Ator makes an interesting observation. She tells Louis Reasoner: “Hey Louis, observe that the number of branches in any tree is always one less than the number of nodes in the tree.” Louis is not convinced that Eva is correct, so he writes the following procedures, each of which takes a tree as an argument (i.e., a list), to verify her claim: (define (is-eva-right? t) (= (- (count-nodes t) 1) (count-branches t))) (define (count-nodes t) (cond ((null? t) 1) ((not (pair? t)) 1) 1 4 2 3
(else (+ (count-nodes (car t)) (count-nodes (cdr t)))))) (define (count-branches t) (cond ((null? t) 0) ((not (pair? t)) 0) (else (+ (count-branches (car t)) (count-branches (cdr t)))))) Eva remarks, “Louis, your count-nodes procedure always returns the number of leaves, not the number of nodes.” Is she right? Circle your answer. YES NO (iii) (2 points) Eva further says, “Your count-branches procedure always computes the wrong answer if the tree is not empty.” Is she right? Circle your answer. YES

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

View Full Document
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 / 8

CS61A_fa02_mt2 - CS 61A, Fall, 2002, Midterm #2, L. Rowe 1....

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

View Full Document
Ask a homework question - tutors are online