{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS61A_sp04_mt2

CS61A_sp04_mt2 - CS 61A Midterm#2 ~77 Your name login csGla...

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

View Full Document Right Arrow Icon
Background image of page 1

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

View Full Document Right Arrow Icon
Background image of page 2
Background image of page 3

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

View Full Document Right Arrow Icon
Background image of page 4
Background image of page 5

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

View Full Document Right Arrow Icon
Background image of page 6
Background image of page 7

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

View Full Document Right Arrow Icon
Background image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 61A Midterm #2 ~77 March 10, 2004 Your name _ login: csGla 1 Discussion section number TA‘s name This exam is worth 40 points, or about 13% of your total course grade. The exam contains six substantive questions. plus the following: Question 0 (1 point): Fill out this front page correctly and put your name and login correctly at the top of each of the following pages. This booklet contains eight numbered pages including the cover page. Put all answers on these pages, please; don’t hand in stray pieces of paper. This is an open book exain. When writing procedures, don’t put in error checks. Assume that you will be given arguments of the correct type. Our expectation is that many of you will not cornplete one or two of these questions. If you find one question especially difficult, leave it for later; start with the ones you find easier. 0 ‘A READ AND SIGN THIS: nH1_ A . . 2 H I certify that my answers to this exam are all my own \ a,” *1: work, and that l have not discussed the exam questions or ‘3 answers with anyone prior to taking this exam. ‘ ‘ ‘ 47 . . . . . 4 If I am taking this exam early, I certify that I shall not ‘ J. discuss the exam questions or answers with anyone until r after the scheduled exain tirne. \Ofi‘ _& 0 ¥‘ {0 T i. . 7. 7 _ . l()l}l,l /_1[) Question 1 (6 points): What will Scheme print in response to the following expressions? If an error message, you may just write “error” the message. Also, draw a box and poin each expression. an expression produces ; you don’t have to provide the exact text of ter diagram for the value produced by (list (cons ’(O) ’(1))m_(apppend ’(2) '(3))) (cdadr ’((a (b)) (c ((e) d) f) (99)) (append ’(list 1 2) ’(BH Your name _ login cs61a Question 2 (7 points): Write the function datum—filter which, given a predicate and a tree, returns a list of all the datums in the tree tl lat satisfy the predicate (in any order). We are using general trees (tum; that can liaye any 111111;;JOI' of children) as defined in lecture. We are 7 binary trees. The function should return the empty I satisfy the pre('li<':ate. You may use helper proced mt using ist for any tree in which no datums ures. For example: (datum—filter even? ( 5 ) ) / \ ( 12 ) C 19 ) / l \ / l \ ( 4 ) ( 22 ) ( 27 ) returns the list (12 4 22) in any order. (define (datum-filter pred tree) Question 3 (7 points): Many people write cond expressions like this: (cond ((= x 5) 100) (else 200)) in\vhkflithenéanronb’hwocond<flanms7thesweond offidnehis2n1else dause Tinseun l)e “mitten as the niore aesthetnndly pleasnlg (if (= x 5) 100 200) \Nks“wnn a procedure.simplify—cond [inn takeszivahd Sehenu‘exlniwsnnizm Ms argu— inenl,znuiixqflacesznitwve«fla1me—“dLh—else cond exlnmmsn)ns by an equivnhun,if exlnxm— snnL lhxn‘nllnnnltinn,cond(»qnesflens(nnilnénesmxk > (simplify-cond ’(+ 3 (cond ((= x 5) 7) (else (let ((y (* X X))) (cond ((= y 16) 2) (else 3))))))) (+ 3 (if (= x 5) 7 (let ((y (* x X))) (if (= y 16) 2 3)))) {fine m an atMnnpt U)xvnte simplify—cond })utit has tvvo lulgs. \Q n kfl)iste find enulfix Umébngs CTheiuflper{neeedunétwo-clauses-cond-exp?is(xnich) ‘Wili AH Hark wHk Mf’rf’d (Nd PXFYUJ/abf (define (simplify-cond exp) . HUI Mi [€{U'n Jymbflmfli”5 ipflvi_ni‘ “I, (if (two-clauses—cond-exp? exp) (list ’if (car (cadr exp)) (cadr (cadr exp)) (cadr (caddr exp))) (define (two—clauses—cond—exp? exp) (and (pair? exp) (eq? (car exp) ’cond) (= (length exp) 3) (eq? (car (caddr exp)) ’else))) Your nznne _ Question 4 (7 points): Here is the constructor for a new ADT designed to store (define (niéxi‘ie—S‘t‘ldonf na'fip .2189 «,‘g‘ (liSt name ’age age (list ’id id))) (a) Define the selectors [or this student data type: (define (name student) 77 4 (define (age student) (define (id student) ¥¥ $4. an. (b) Write get-student, which takes a list of students and 2t student who matches that lD. If no student in the list niu #f. You may not define any helpers other than the select login csb‘lar student inior‘lnettion. an ll) as arguments, and returns tches the ID, the function returns ,‘ors you wrote in part (:1). (define (get-student list—of-students student—id) Question 5 (6 points): Here is the code for the dyadic version of the sentence procedure using conventional style: (define (sentence arg1 arg2) (cond ((and (word? argl) (word? arg2)) (list arg1 arg2)) ((and (word? argl) (sentence? arg2)) (cons arg1 arg2)) ((and (sentence? argl) (word? arg2)) (append argl (list arg2))) (else (append argl arg2)))) \\l§112gn)n1g't()11»«lesipgi thlSI)IU(XKlHIlé115H1g‘(hltn <hiix1(wl Inw>grzuiin1nig. \\R2Wl se(%t)1>e fignnhuessudins(word word)(n'(sentence word) uniemxnnhng14>Hn*uve;ugunnuus Misentence li}1tll(flf tliziii n(l(li11g§ e:(1)li(:it tgy1)(3 tziigs tl) \V()Y(lS 2L11(l s<311t()iiC(zs, \A’Cill 11s<2 tll(! ;i('t11;Ll ES<l1cu11(* tyq)es as nIn)h(jt tags, b§’llnvrlthlg the selectors fer tzngge(l(latn. (ltlns relnw;(ni th<ztnxi that the explicit types we’ve used all end up as improper lists such as (rational 3 . 4), a)sentence?isfabefortheni) (define (type-tag thing) (cond ((sentence? thing) ’sentence) ((word?'thing) ’word) (else (car thing)))) (define (contents thing) (cond ((sentence? thing) thing) ((word? thing) thing) (else (cdr thing)))) Youriunvxuisnnicfi sentence shouhlluduum:exacdy'hhetln:ene dehned nhove.llen%nrc sennzexannnes (sentence ’george ’harrison) => (george harrison) (sentence ’angus ’(young)) => (angus young) (sentence ’(eric) ’(clapton)) => (eric Clapton) This question continues on the next page! 0 Your name . fi_ Iogiu (1561;; 5 Question 5 continued: (a) Make calls to put to prepare the table: (1)) \Vrito the new sentence procedure in the Dam Directed Programming styl 0. You may use apply—generic from the book if you find it, useful Question 6 (6 points): Ilercistlnaeval-l code Rn scheme—1 (beflnriany’chang055Kniinadeiiihn)(n‘hoinowork} (define (eval-l exp) (cond ((Constant? exp) exp) ((symbol? exp) (eval exp)) ; use underlylng Seheme‘s EVAT ((quote-exp? exp) (cadr exp)) ((if-exp? exp) (if (eval-l (cadr exp)) (eval-l (caddr exp)) (eval-l (cadddr exp)))) ((lambda—exp? exp) exp) ((pair? exp) (apply-1 (eval—l (car exp)) ; eval the operator (map eval-l (cdr exp)))) (else (error “bad expr: “ exp)))) Suppose we moved the 00nd clause beginning with pair? (the one that handles function calls) to UK? beginning of the cond, making it the first, clause: (cond ((pair? exp) ...) ((constant? exp) ...) ((symbol? exp) ...) (else (error ...))) \VNJiHntabovechangmgshow whatscheme-l\wnndinnmgnvm1thefiflknflnginpnmxll tlio r051fl1,is alifSWlk elix)r.jiist \VIito "lClll{()ll”: 3 => + => (if #t 1 2) => ((lambda (x) (* x x)) 3) => (+ 3 4) => (lambda (x) (* x x)) => ...
View Full Document

{[ snackBarMessage ]}