CS61A_sp95_f_sol

CS61A_sp95_f_sol - CS61A: Spring 1995 Final solutions CS...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 61A Spring 1995 Final exam solutions 1. prefix-to-infix, tree-reorder (a) (define (prefix-to-infix tree) (if (atom? tree) tree (list (prefix-to-infix (cadr tree)) (car tree) (prefix-to-infix (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 (tree-reorder ordering tree) (if (atom? tree) tree (map (LAMBDA (POS) (TREE-REORDER 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 (tree-reorder 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 (pos-help str 0))) (define (pos-help str index) (if (empty-stream? str) the-empty-stream (CONS-STREAM (nonzeros (head str) index) CS61A: Spring 1995 Final solutions 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
(define (nonzeros lst index) (nz-help lst index 0)) (define (nz-help lst index subindex) (cond ((null? lst) the-empty-stream) ((= (car lst) 0) (nz-help (cdr lst) index (+ subindex 1))) (else (cons-stream (list index subindex) (nz-help (cdr lst) index (+ subindex 1)))))) The crucial point in this solution is that the capitalized CONS-STREAM in pos-help can't be append-streams 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 (empty-stream? str) the-empty-stream (pos-help str (head str) 0 0))) (define (pos-help str lst index subindex) (cond ((empty-stream? str) the-empty-stream) ((null? lst) (pos-help (tail str) (head (tail str)) (+ index 1) 0)) ((= (car lst) 0) (pos-help str (cdr lst) index (+ subindex 1))) (else (cons-stream (list index subindex) (pos-help str (cdr lst) index (+ subindex 1)))))) Scoring: 5 ok 4 uses interleave or append-streams, not flatten or interleave-delayed 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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

CS61A_sp95_f_sol - CS61A: Spring 1995 Final solutions CS...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online