




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





CS 61A, Fall, 2002, Midterm #2, L. Rowe
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)))
a) d)
b)
e)
c)
(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)
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)
which numbers to remove. Continue this process until the end of that list is reached. At that point, the list contains only simple numbers. For example, (all-simple-numbers 5) => ( 2 3 5) We have given you a code skeleton for this procedure. You are to fill in the blanks so that the procedure (all-simple-numbers n) returns a list of simple numbers les that or equal to n. Remember, 1 is not simple, so the list begins with 2.
(define (enumerate-interval m n) (if (> m n) nil (cons m (enumerate0interval (+ m 1) n)))) (define (all-simple-numbers n) (define (helper remaining) (if (null? remaining) nil (cons ______________________ (helper (filter
________________________________________________)))))
(helper (enumerate-interval 2 n)))
Hint: you might find the remainder procedure useful.
The empty tree is represented by nil (i.e., the empty list). For example, the following code builds the tree shown on the right:
(define two (make-tree 2 โ() โ())) (define one (make-tree 1 โ() โ())) (define three (make-tree 3 two one)) (define seven (make-tree 7 โ() โ())) (define four (make-tree 4 three seven))
A catamorphism is a function that abstracts recursion over a structure. For example, the function accumulate described in the book is a catamorphism that operates on lists. You
are going to complete the procedure acc-tree that will work on binary trees similar to the way accumulate works on lists. acc-tree will take three arguments:
For example, if we have a tree with numbers at each node entry, the call (acc-tree + 0 four) => 17 For the tree constructed above. You may not use set! procedures to answer this problem.
(i) (8 points) Fill-in the blanks to complete the definition of acc-tree. (define (acc-tree op init tree) (if (null? tree)
(op ( ________________________________ )
( ________________________________ )
( ________________________________ ))))
(ii) (6 points) Use acc-tree to write the procedure map-tree that will take as arguments a procedure and a tree and return a new tree in which the procedure has been applied to every entry in the argument tree. Fill-in the blanks in the following code: (define (map-tree op tree) (define (map-op entry left right) _________________________________ )
(acc-tree ________________________________ ))
(iii) (6 points) Use your acc-tree procedure to convert a tree to a list of entries in infix, prefix, and postfix order. Complete the procedures infix-op, prefix-op, and postfix-op to complete the following uses of acc-tree. (acc-tree infix-op nil tree) (acc-tree prefix-op nil tree) (acc-tree postfix-op nil tree)
(define (infix-op entry left right) ( ___________________________________ ))
(define (prefix-op entry left right) ( ___________________________________ ))
constructor make-matrix-with-same? that returns a matrix with the additional procedure same?. For example, (define m1 (make-matrix-with-same? 3 4 โ(1 4 3 0) โ(5 0 1 0) โ(7 0 0 1))) (define m2 (make-matrix-with-same? 3 4 โ(1 4 3 0) โ(5 0 1 0) โ(7 0 0 1))) (define m3 (make-matrix with same? 2 4 โ(1 4 3 0) โ(5 0 1 0))) (m1 โnum-rows) => 3 (m1 โnum-cols) => 4 (m1 โentry 2 0) => 7 (m1 โsame? m2) => #t (m1 โsame? m3) => #f
Fill-in the blanks for the following definitions of make-matrix-with-same?: (define (make-matrix-with-same? nrows ncols. rows) (let ((orig-matrix (apply make-matrix (append (list nrows ncols) rows)))) (define (row-same? m rownum) ; m is matrix (define (helper column) ; check each col of row (if (< colnum 0) #t
______________________________________ )) (helper (- ncols 1)))
(define (matrix-same? m) ; m is second arg to โsame? (define (helper rownum) ; check each row (if (< rownum 0) #t (and (row-same? m rownum) (helper (- rownum 1))))) (helper (- nrows 1))) (lambda (op. args) (if (eq? op โsame?) (let ((m (car args))) (if (and (= (m โnum-rows) (orig-matrix โnum-rows)) (= (m โnum-cols) (orig-matrix โnum-cols))) (matrix-same? m) #f))