{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

mt2_sol

# mt2_sol - M IDTERM E XAMINATION S OLUTIONS C OMPUTER S...

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

M IDTERM E XAMINATION S OLUTIONS 2 C OMPUTER S CIENCE 61A Department of Electrical Engineering and Computer Sciences University of California, Berkeley July 29, 2010 Throughout the exam, we used comment codes. In other words, if you see something like C or γ written on your exam, it does not indicate your letter grade, but instead you should look up what that comment means on this solution document! 2 Vector Shuffling A useful approach in vector-based questions is to employ a helper procedure that keeps track of our current position in the vector, as we process the vector. Our solution was (define (knuth-shuffle! vect) (define (knuth-shuffle-helper vect i) (let ((vect-length (vector-length vect))) (if (= i vect-length) ’okay (begin ;; Choose a random position to swap with. (let ((j (+ i (random (- vect-length i))))) ;; Remember the element at that position. (let ((old (vector-ref vect j))) ;; Swap the two elements (vector-set! vect j (vector-ref vect i)) (vector-set! vect i old))) (knuth-shuffle-helper vect (+ i 1)))))) ;; Call the helper procedure. (knuth-shuffle-helper vect 0)) Grading : 2 points if your solution is correct; 1 point if your solution has the idea, such as saving data and performing a swap, but generating a random number from 0 to the length, as opposed to from j to the length; 0 points for an incorrect solution. Comments : α : Always generates random numbers that span the length of the vector, rather than from i to the end. 1

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

View Full Document
1 Box and Pointers +---+---+ +---+---+ a ->| | | --+-+XX>| | | / | +-+-+---+ | +-+-+---+ +-> 7 v | x 5 v v 2 +---+---+ +---->| | | / | | +-+-+---+ | | | v | 1 | | +---+---+ +---+---+ | c ->| | | --+---->| | | --+---+ | +-+-+---+ +-+-+---+ | | | v | +-------+ 2 | | +----------------------+ v +---+---+ +---+---+ b-x>| | | --+---->| | | / | | +-+-+---+ +-+-+---+ | | | | v v | 3 4 | +->+---+---+ +---+---+ | | | --+--->| | | / | +-+-+---+ +-+-+---+ +->6 | x | v v 4 6 Grading : 2 points, 1 point each for each box-and-pointer final result. Half-a-point off if the list c was deep copied: remember that the append procedure only copies the ‘backbone’ of the first list. Comments : α : Box-and-pointer diagram matches incorrect output. 3 Object-Oriented Programming This was an above-the-line OOP question, but the main idea of the question was remembering that lists are simply pairs, whose second elements are also lists. In this manner, the list class could be defined as (define-class (list car cdr) (method (map fn) (instantiate list (fn car) (ask cdr ’map fn)))) 2
This works because the cdr of a list is also a list, so we can trust that passing it the map message would pro- duce another list, with the function fn mapped to each element of the cdr . The final piece of the puzzle was the empty-list class, which was simply (define-class (empty-list) (method (map fn) self)) A common mistake was to instead define the empty-list class as (define-class (empty-list) (method (map fn) ’())) but this reflected a domain-and-range confusion. One example where this would not work is (define squared-ls (ask ls ’map square)) (ask squared-ls ’map square) where ls is defined as in the question. If an empty-list object merely returned the empty list when it was passed the map message, then when squared-ls is passed the map message, it would try to pass the empty list (the list, not an object of the empty-list class) the map message. In effect, we would eventually error at the call (ask ’() ’map square) .

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.

{[ snackBarMessage ]}