CS61A_fa02_mt2

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

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
(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
Image of page 2
(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.
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