中国高校课件下载中心 》 教学资源 》 大学文库

《Structure and Interpretation of Computer Programs》q1 sp05

文档信息
资源类别:文库
文档格式:PDF
文档页数:12
文件大小:101.9KB
团购合买:点击进入团购
内容简介
Closed book- one sheet of notes hroug hout this quiz, we have set aside space in which you should write your answers. Please try to put all of answers in the designated spaces, as we will look only in this spaces when grading
刷新页面文档预览

MASSACHVSETTS INSTITVTE OF TECHNOLOGY Depart ment of Electrical Engineering and Computer Science 01-Structure and Interpret at ion of Computer Programs Spring semester, 2005 Quiz i Closed book- one sheet of notes hroug hout this quiz, we have set aside space in which you should write your answers. Please try to put all of answers in the designated spaces, as we will look only in this spaces when grading Note that any procedures or code fragment s that you write will be judged not only on correct function, but also on clarity and good programming practice. NAME Sect ion number Tutor’ s Name: PART Value Grade grader 2345 叼56 Tot a 100

￾                 !"##￾$        %&  &' (##) ￾          * *  *& + ,' - *. & & &  -** / &*  - /  &-&" %& /      /  &-&  * & &&' & - - 0 /  *& && -* "  * /  &   & * / - - 1 2   /    ' 1  &  /    " 3  13 4& 3 %5     ￾ (# ( ￾6 7 (# 8 ￾( ) ￾) ! ￾!  ￾##

6.001, Spring Semester, 2005-Quiz I Part 1:(20 points) kor fach of thf following fxprfssions or sfqufncfs of fxprfssions, st atf thf valuf rft urnfd as thf rfsult of fvaluat ing thf final fxprfssion in fach sft, or indicate that thf fvaluat ion rfsults in an frror. Ef thf rfsult is an frror, st atf in gfnfral terms w hat kind of frror (f g. you might writf frror: wrong typf of argument to procfdurf"). ef thf fvaluat ion rft urns a built-in pro cfdur writf primitive pr ocedure. ef thf fvaluation returns a usfr-crfat fd procfdurf, writf compound pr oce Ef thf fxprfssion dofs not rfsult in an frror, also st atf thf "typf" of thf rfturnfd fxprfssion, using thf notat ion introduced in lfct urf hou may assumf that fvaluation of fach sfqufncf takfs placf in a nf wly init ializfd zchfmf syst fm Question 1. I aluf ypf Question (define + - (*12(+62) I aluf s ypf Question 3. ((lamb da(a b)(+ b a)) I aluf

￾        (       *  * - >&&&  &+ &  >&&&' & * .    & * &   .  * ? >&&  * &'   * * .  & &   "  * &  &  ' &   & -* 0   @"" / * - A3 - /      BC"  * .   &  1 9  ' - ￾ ￾  "  * .   &  &9  ' - ￾ ￾  "  * >&& &  &    ' & & * A/B  *   >&&' & *      "  / &&  * .   * &+  0&    -/ , * &/&" ￾   ￾  3 /3 ￾           3 /3 ￾           3 /3

6.001, Spring Semester, 2005--Quiz I Question 4. ((lamb da (a) (+(sart a)(sart b)))) Question 5. e arg (defil (define (proc arg) (let ((local-arg 2)) (list arg local-arg))) (pr oc 1) Valu ype

￾        7 ￾             3 /3 ￾                       3 /3

6.001, Spring Semester, 2005--Quiz I Cons. 4er the follow ng S mple database of personnel nformat on. s he ent.re 4at abase. s represented as a liit of entr es. oach entry .s made 5s. ng the follow ng constr5ctor 01, SpI.-5k le lptry p Irzop z 5251 St So 2szt p Irzop z525r s he pn(10- part of an entry . s created 5s. ng the constr 5ctor Note that vales for names an4 pos t ons are represent e4 5s. ng str ngs, wh. le salar. es are represented 5s. ng n5mbers. Here s an example 4at abase 01,Sp1z5-p2105t5 2zt .-5k le lptry . -5k lep Irzopz-Sth""johphlpry"n 30000"pr lz solpt"n -5klelptry, ok ep rop"z-Sth m ppI"-fr SIm"h15thIr"n toooo"hick Ir"n 5k le lp 5k lep Ir r 10"n 55000"hack Ir"n -5k le lptry . -5k lep Irzop"Oo 1""j5pl""12Sz5(lth"n 38000"5zz Szt5pt Irzop"rol-5rSij5pl"n s9000"v Sc lepr 1z solpt Note that the fam. ly"name. s always the first element of the person abstract. on, b5t there can be arb. trar.ly many"g.ven"names Queltion 6: t raw a box-an4-po nter 4. agram for the str5ct5re correspond ng to fn1f, where 01, SpI t lzt.-5k le lptry -sk lep Irzop "z-Sth""johp'hlpry"n 30000 "pr Iz solpt"nm

￾        8 & * - & 1&  & " *  1& & & &     &" * / &  & * - & 3           * ￾     / &  & * & 3      * . &  &  &&  & & &&' -* &&  & &  1&"  &  > 1&3        !"! !#"! !" ! $$$$ !!     !#! !! !! !""! $$$$ !"!     !"! !! $$$ !"!     !! !#! !%"! &$$$ !!     !! !! !#! '$$$ !( !  * * B/B  & -/& * ?&   * & 1&' 1  *  1 1/ / B.B &"   !   ￾  " -  1>99   * &   &   ' -*       !"! !#"! !" ! $$$$ !!

6.001, Spring Semester, 2005--Quiz I Question 7mt omplete the n-f(2 abstraction by providing selectors for >n(1*-, for 1616(2 and for >*1dfd*-. For example (person test Value: ("smith""john"henry ") Question 8t omplete the >n(1*-abstract ion by providing selectors for a6ed12m-6en and bdin-m-6en1 (given-names (make-person "jones""anne""marie""heather")) value: ("anne ""marie""heather " (family-name (make-person "jones""anne""marie""heather ") Jones c hus the family name is always the first name in the entry, the given names are any names after

￾        ) ￾  !  *   1& 1/ . &&  ￾  '     ￾  "  >3   )*+, !"! !#"! !" ! ￾  #  * ￾  1& 1/ . &&        ' "" (    !#! !! !! !""! )*+, !! !! !""!     !#! !! !! !""! )*+, !#! * & * /  & -/& * ?&   * /' * . &  / &  *&"

6.001, Spring Semester, 2005--Quiz I Part 3:(20 points) Now suppose that we want to retrieve entries from the dat abase that satisfy cert ain constraint s For example, we might want to get all the entries of people wit h the posit might want to get the entries of people whose first name is"Jane", or all the entries of people with aries bet ween 25000 and 50000. Remember our procedure (define (filter pred lst) ond ((null? lst) nil) ((pred (car lst))(cons (car lst) (filter pred (cdr lst)))) (else (filter pred (cdr lst))))) Question 9: We want a way of get ting entries from the dat abase with a particular first name (where by first name, we mean the first of the "given"names, not the family name). You may assume that every entry in the database has at least one given name. You may also find the procedure string=?, which compares two strings, to be useful Complete the follow ing code so that, for example (filter (called-by "jane") sampledata) Value: (((doe""jane ""elizabeth")38000"assistant") (define (called-by Question 10: Suppose we want to find all the people who have salaries at least as large as a specified amount, and who hold a particular posit ion (filter (salary-and-position 60000 hacker")sampledat a) Value: ((("jones""anne""marie""heather")60000"hacker")) Complete the following code fragment (define (salary-and-position minimum posn)

￾        !      - & & * - -  . &  * 1& * &&/  &&"  >' - * -    * &   -* * &  B*0B'  - * -   * &   -*& ?&  & BDB'   * &   -* && 1- ()###  )####" 51    3      +-                  ￾  $ '    !#!  )*+, !! !#! !%"! &$$$ !!     ￾    & - -  ?  *  -* *. &&  & &  &  &?  '  -* *    &"     $$$$ !"!  )*+, !#! !! !! !""! $$$$ !"!  * -  3     + 

6.001, Spring Semester, 2005--Quiz I Question 11: Assume that the procedure one-of has the following behavior. It takes two argu- ments, an element and a list. It successively test s for equality of t he element to entries in the list using string=?, unt il it eit her reaches the end of the list(in which case it ret urns false)or until it finds an element of the list that mat ches(in which case it ret urns true). Using this, supply an expression for INSErT1 in the expression below. (define (has-name name) IN SERT1) (filter(has-name "marie") samp ledata) Value: ((("jones""anne""marie""heather")60000"hacker") (("roe""marie""jane")29000"vice-president")) Question 12: Recall the pro cedure (define(map proc lst (if (null? lst nil (cons (proc (car lst))(map proc (cdr lst))))) Provide an expression for INSERT2 so that evaluating(map INSERT2 sampledat a) would ret urn a list of the number of names(bot h family and given names) for each entry, e. g (map IN SERT 2 samp ledata) Value:(34233) You may assume that length is a procedure t hat ret urns the number of elements(or lengt h) of a

￾        6 ￾   &&  * *     *& * - 1*."  0& -  9 &'     &"  & &&./ &&  + /  *   &  * &' &  '   * *& *   * & @ -** &   & &C    ?&    * & * *& @ -** &   &  C" E& *&' & /  >&&    * >&& 1-"  "   ./0123  "  !!  )*+, !#! !! !! !""! $$$$ !"! !! !! !#! '$$$ !( ! ￾   5 *  3      +-           %.  >&&   & * .  ￾  ￾  -     &  *  1  & @1* /  . &C  * /' ""  ./0123  )*+,       / &&  *   &    *  & *  1  & @ *C   &"

6.001, Spring Semester, 2005--Quiz I Part 4(12 point s) Given our little data base of personnel files, we might want to be able to sort the entries, for example by increasing salary. Here is a procedure for sort in (define (find-best best rest comp are extract or (if (null? rest) (if (compare (extractor (car rest)) (extractor best t (find-best best (cdr rest) comp are extract or ))) (define (remove elt rest same) (if (null? rest) (if (same elt (car rest)) (cons (car rest)(remove elt (cdr rest) same))))) (define (sort data comp are extractor same) let ((trial (find-best (car data)(cdr data) comp are extract or))) (let ((rest (remove trial data same))) (cons trial (sort rest compare extractor same)))))) For example, to sort our data by increasing salary, we would evaluat ledata salary = We are going to measure the order of growth in time(as measured by the number of primit ive operat ions in the computation) and in space(as measured by the maximum number of deferred operat ions-do not count in space the intermediate dat a structures constructed by the algorit hm) measured as a funct ion of t he size of dat a, denoted by n. Assume that the procedures used for compare, extract or and same use const ant time and space For each of the following questions, choose the descript ion from these options that best describes the order of growt h of the process. If you select"something else", please st ate why C: exponential D: quadrat ic E: logarit hm F: somet hing else

￾        F      .     1&  & ?&' - * -  1 1  & * &'  > 1/ & &/"  &     &3       4  +-     4   4         4       4  (     +-             (         4           4   (     +-         4   >'  &    1/ & &/' - -  . 3   5  6    1   & G      & *   &  & &  1/ * *C' &  &     * &,  '  1/ " &&  * *  & &  ￾! "    & &   &"  *  * - + &&' *& * &  *& & * 1& &1& *   -*  * &&"  / & A&* &B' & & -*/" 3 & ;3  3 > 3 +  3 * 3 &* &

6.001, Spring Semester, 2005--Quiz I Question 13: What is the order of growth in time of the procedure find-best? Question 14: What is the order of growth in space of the procedure find-best? Question 15: What is the order of growth in time of the procedure remove? Question 16: What is t he order of growth in space of the procedure remove? Question 17: What is the order of growth in time of the procedure sort? Remember to include t he effect of find-best and remove Question 18: What is the order of growt h in space of t he pro cedure sort? Remember to include t he effect of find-best and r e

￾        H ￾  3 <* & *   -*    *    # I ￾  3 <* & *   -*  &  *    # I ￾   3 <* & *   -*    *   I ￾  "3 <* & *   -*  &  *   I ￾  !3 <* & *   -*    *   I 51    * J   #   " ￾  #3 <* & *   -*  &  *   I 51    * J   #   "

6.001, Spring Semester, 2005--Quiz I Parf 5(15 po infs Here is a procedure for composing two procedures uiefine comose f ge tmbi t ue uf Ug xeeee and here is a procedure for applying a given procedure some number of times uiefine ure3ettei f ne uif u n 1 CODE-ADDIT ION-HEREee You will consider some possible pieces of co de to complete. ppt, pn. The expected behav ior is: uiefine us lutre xe uo x xee Value: 7slutre -Q#[com 3ouni-3roceiure 7 slutrel7 reset tei slutre 4e Vtlue: #[com3ouni-3roceiure 8] uur e3ettei s lutre 3e 2e vtl onl 25 uur e3ettei s lutre 4e 2e vt lue: n553n uur e3ettei s lutre 2e 3e It lue: 81

￾        ￾#      &     & -  &3      4   4  * &     /  .   &  1  &      6   7891 :99.3.8/ ;121  - & & &&1 &     ￾ " * > 1*. &3  + 4  4 4 )*+, !+ ￾ +?!  +  )*+, <=+ + &?  +   )*+,   +   )*+,   +   )*+, &

共12页,试读已结束,阅读完整版请下载
刷新页面下载完整文档
VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
相关文档