




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Main points of this exam paper are: Leaf Node, Arrow Diagrams, Accumulate Cons, Scheme Expressions, Tree Connects, Two Nodes, Arrow Diagram, Interesting Observation, Simple Numbers, Algorithm
Typology: Exams
1 / 8
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))