Prelim 1 Fall 1997 1. (10 pts) For each of the following expressions, determine what value would be returned by a Dylan interpreter, or explain what kind of problem would occur. (a) (bind (((f ) car) ((x ) 10)) (f (cons x f))) ==> 10 (b) (bind (((f ) (method (x) (+ x 1))) ((x ) 10)) (f x)) ==> 3 (c) (bind (((x ) 6)) (bind (((x ) 7) ((f ) (method ((x )) x))) (+ x (f x)))) ==> 14 (d) (bind (((dino ) cdr) ((barney ) (method (x) (cons x 42)))) (barney (dino (barney dino)))) ==> (42.42) (e) (bind (((f ) '(method (x) (+ x 1))) ((x ) 2)) (f x)) ==> Error: first element in combination must evaluate to a function 2. (15 pts) Suppose we define a list godzilla by (define (godzilla ) (a) Draw the box and pointer diagram corresponding to gorilla. [|][-]->[|][-]->[|][-]->[|][/] | | | | 'who 'does [|][/] [|][-]->[|][/] | | | 'not 'like 'barney (b) What is the value of (car (cdr (cdr godzilla)))? ==> (not!) (c) Define a procedure lunch that only uses car and cdr such that (lunch godzilla) returns (barney). ==> (define lunch (method ((l )) (cdr (car (cdr (cdr (cdr l))))))) 3. (a) (car barney) type: exp: (list 42) (b) (barney 'car) type: exp: (method (x) 42) (c) ((barney car)) type: exp: (method (x) (method () 42)) (d) (barney (barney barney)) type: exp: (method (x) 42) (e) ((method () (barney)) type: exp: (method () 42) 4. I'll type the proof up later. (b) T(n) = T(n-1) + O(n) + c (c) O(n^2) 5. (35 pts) (a) (define (count ) (method ((x ) (l )) (cond ((null? l) 0) ((= (head l) x) (+ 1 (count x (tail l)))) (else: (count x (tail l)))))) (b) (define (count ) (method ((x ) (l )) (accumulate 0 (method (o (total )) (if (= x o) (inc total) total)) l))) (c) (define (map ) (method ((f ) (l )) (accumulate '() (method (x (result )) (cons (f x) result)) l))) (d) (define (filter ) (method ((f ) (l )) (accumulate '() (method (x (result )) (if (f x) (cons x result) result)) l))) (e) (define (compose-list ) (method ((l )) (accumulate (method (x) x) compose l)))