This preview shows pages 1–3. Sign up to view the full content.
CS 61A
Spring 1995
Final exam solutions
1.
prefixtoinfix, treereorder
(a)
(define (prefixtoinfix tree)
(if (atom? tree)
tree
(list (prefixtoinfix (cadr tree))
(car tree)
(prefixtoinfix (caddr tree)))))
Scoring:
2 if correct, 1 if a tree recursive function, 0 if mutation used.
Most people did okay on this part, but many solutions were more complicated
than necessary because of not believing in leaf nodes as the base case.
(b)
(define (treereorder ordering tree)
(if (atom? tree)
tree
(map (LAMBDA (POS) (TREEREORDER ORDERING (NTH POS TREE)))
ordering)))
Scoring:
3 if correct, 2 if tree recursive, 1 if (lambda (number) .
..),
1 if map inside map, 0 if lambda's argument unused (or no arg),
0 if (lambda (list) .
..) or no lambda at all,
0 if code works only for examples shown.
A fairly common wrong answer was
(lambda (pos) (nth pos (treereorder ordering tree)))
which of course is an infinite recursion since the problem never gets
smaller!
2.
streams
The solution we were expecting was this:
(define (positions str)
(FLATTEN (poshelp str 0)))
(define (poshelp str index)
(if (emptystream? str)
theemptystream
(CONSSTREAM (nonzeros (head str) index)
CS61A: Spring 1995 Final solutions
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
(define (nonzeros lst index)
(nzhelp lst index 0))
(define (nzhelp lst index subindex)
(cond ((null? lst) theemptystream)
((= (car lst) 0) (nzhelp (cdr lst) index (+ subindex 1)))
(else (consstream (list index subindex)
(nzhelp (cdr lst) index (+ subindex 1))))))
The crucial point in this solution is that the capitalized CONSSTREAM
in poshelp can't be appendstreams or interleave, because those would
fail to delay the recursive call.
The call to FLATTEN in positions
turns the stream of streams of positions into a flat stream of positions
as desired.
Another possible solution essentially does the flattening as it goes:
(define (positions str)
(if (emptystream? str)
theemptystream
(poshelp str (head str) 0 0)))
(define (poshelp str lst index subindex)
(cond ((emptystream? str) theemptystream)
((null? lst)
(poshelp (tail str) (head (tail str)) (+ index 1) 0))
((= (car lst) 0)
(poshelp str (cdr lst) index (+ subindex 1)))
(else (consstream (list index subindex)
(poshelp str (cdr lst) index (+ subindex 1))))))
Scoring:
5
ok
4
uses interleave or appendstreams, not flatten or interleavedelayed
3
gives stream of streams as result, or uses (list) append
2
leaves out some positions, e.g., only gets one per list
1
solves wrong problem: no positions at all
0
uses mutation
1 from above if no base case (stream must be infinite)
1 from above if gets lists and streams mixed up (e.g., takes car of stream)
3.
Metacircular evaluator and Logo
(a)
How do these procedures interact with dynamic scope?
to circle.area :radius
This is the end of the preview. Sign up
to
access the rest of the document.
 Fall '08
 Harvey
 Computer Programming

Click to edit the document details