当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

《Structure and Interpretation of Computer Programs》q2 sp 05

资源类别:文库,文档格式:PDF,文档页数:21,文件大小:128.19KB,团购合买
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 it Closed book two sheets of not es Throug hout this quiz, we have set aside space in which you should write your answers. Please try
点击下载完整版文档(PDF)

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 it Closed book two sheets of not es Throug hout this quiz, we have set aside space in which you should write your answers. Please try to put all of your answers in the designated spaces, as we will look only in t his 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: Section Number Tutor's n: PART Value grade Grader 15 100

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

6.001, Spring Semester, 2005--Quiz II Part 1:(25 points) We are going to explore a new kind of data structure, called a cycle. You can think of this as a kind of circular list: indeed we are going to represent a cy cle as a loop of cons cells, where the car of each cell point s to a value, and the cdr of each cell points to the next element in the circular sequence. To create a cycle from a list, we can use (define (list->cycle lst) et-cdr! (last lst) lst) (define (last lst) (if (null? lst) (error "not long enough") (if (null? (cdr lst)) (last (cdr lst))))) (define test-cycle (list->cycle (a b c g))) To see what this struct ure looks like, you might find it convenient to draw a box-and-pointer Associated wit h a cycle, we have several operat ions head will ret urn the value stored at the cell in the cy cle pointed to by its argument, e. g (head test-cycle)will return the value a rotate-left willrot ate t he cycle one element to the left, e.g,(head (rotate-left test-cycle)) will return the value b; (you can think of a cycle as a circular list in which the arranged in clo ckwise sequence, the head is at the top, and"left means rot at ing the sequence counter-clockwise) rotate-right will rot ate the cycle one element to the right, in analogy to rot ate-left, e. g (head (rot ate-right test-cy cle)) will ret urn t he value

￾       '     :     ￾     ,  ) .    ) & !!&       ￾ ￾  ,  ) - > ?.  )/   . %   %  ,)) ) %    /,% %*& ) ) %  ) &  @A %  ) %* 7/,%B> ￾     ,  ) .    ) )&  .      & !!&       ￾ ￾  ,  ) - !

6.001, Spring Semester, 2005--Quiz Il Defining head is easy (define head car) Question 1: Write the pro cedure rotate-left. Be sure that it ret urns a pointer to the right cell the sequence. You may assume that the argument is a non-empty cycle. Question 2: Write the pro cedure rotate-right. Be sure t hat it returns a pointer to the right cell in the sequence. You may assume that the argument is a non-empty cycle

￾       5 =  % %.2 ￾   ￾   : )      ! 9 % )  %    ) )   ) %*!  . %% ) )  %  7. .! ￾   : )      ! 9 % )  %    ) )   ) %*!  . %% ) )  %  7. .!

6.001, Spring Semester, 2005--Quiz Il Now we want to add a new element to a cycle. This means we want to insert a new cons cell, wit h the supplied argument as its car, before the lo cat ion in the cy cle current ly pointed to by the supplied argument. We are going to treat this as a mut ator(that is, it changes an exist ing dat a struct ure), so we do not want to rely on the value ret urned by the procedure. Hence by convention, let's have the procedure ret urn the sy mbol done. Here is an example behavior define test-cycle (list->cycle '(a b c g))) Value: test-cycle (head test-cy cle) head (rotate-left (rotate-left test -cycle))) Value: c (insert-cy cle! (rotate-left (rotate-left test-cycle))'x)) d test-cy cle head (rotate-left (rotate-left test-cycle))) Value: x head (rotate-left (rotate-left (rotate-left test-cy cle)))) Value: c non-empty cycle. You should use rotate-left and /or rotat e-right where approprlate nt is a Question 3: Write the procedure insert-cycle!. You may assume that the argume

￾       6 , , ,    ,    .! )% % , ,  %  , ￾ & ,) ) %  % % ￾& 0 )   ) . .   0. ) % ! :     )% %   ?) %&  )%  <%  %B& % ,   ,  .  ) -  0. ) ! & 0. -& 3% )- )   ) %.0 !  %  < 0)-2 ￾   ￾  ￾      ￾     ￾ ￾  ￾     ￾  ￾  ￾       ￾     ￾ ￾  ￾      ￾ ￾  ￾  ￾     ￾   : )   ￾ ￾ !  . %% ) )  %  7. .!  %) %     C     ,) !

6.001, Spring Semester, 2005--Quiz Il Finally, suppose we want to remove an element from the cycle. Our goal is to remove from the cycle the cell to which the supplied argument points, as well as any pointers into the cycle from removed cells. Note that if we remove the cell to which a global variable refers, we may break our data struct ure, but we will assume that it is the responsibility of t he user to manage pointers into the cycle Question 4: Provide a definit ion for the procedure delet e-cycle!. Use rotate-right, rotate-left head and set-cdr but no other list operations. Also be sure that you change the cdr of the cycl cell t hat contained the deleted value to the empty list

￾       ( .& %% , ,  -    ) .!   %  -  ) . )   ,)) ) %  %& % , % . %  ) .  - %!  )  , - )   ,))  0 -0 %& , . 0/   %& 0 , , %% )  % ) %%0.  ) %   %  ) .! ￾   $-  =  )     ￾ ￾ ! D%              ￾ 0  ) % %! % 0 % ) . ) ) ￾  ) .  )  )  -  ) . %!

cansardhent r-y-lt-ha fnn5i g! ez Il uzIt 2:(30 pr(Qti) Ju howu x'grbdu: ud ghu: b: upg b; h ugogb'(ug, sst!)x' gb bur Vo guogu, whx h oWbws us gb ho'gu ghu bx'dx'gs b; vorxobWus. fbh ugh us xg wbuld bu 'xu gp bu obW gb u?db b?u b; ghusu h ugogxb's, whx: h h uo's wu wb uvd ,uud sbh u woy b; ruh uh burx'g whog ghu pruvxb us voWuu wo i b'Sxdur ghu ;bWbwx'g: b cu (define (set! -start val) (define old 1O) (let ((current val)) (cond ((eq? action value) current (set! old (cons current old)) rent (car new)) current) ((eq? action 'reset) (set! current (car old)) (set! old (cdr old)) else (error "i dont know how to do" action))))) (define (set!-careful var val) (var ]new val)) (define (set! -undo var) (var reset)) Udur ghs )uw s: huh u, o vorxobWu xs o: guoWy o prb: uduru, sb gb gug xgs voWuu, wu jusg uud gb sud (define foo (set!-start 5) (set!-careful foo 10 (set!-careful foo 15) (set!-undo foo)

￾         !  : )-  )    ?!!&  B   & ,)) ,% %  ) ) 0%  -0%! %  , 0   0 0     )% %& ,)) % , ,  % ,.  0 ,) ) -% - ,%! % ) , 2 ￾ ￾   ￾  ￾ ￾ ￾￾   ￾ ￾    ￾  ￾￾!     ￾￾!    ￾  ￾    ￾  ￾    ￾￾!    ￾  ￾   ￾  ￾    ￾ ￾ "  #      ￾ ￾    ￾   ￾ ￾   ￾  D )% , %)&  -0 % .  & %   % -& , 1%   %   %%&  <2 ￾  ￾  $ ￾   $ ￾   %& ￾   %& ￾   %$ ￾   %$ ￾   ￾   %&

6.001, Spring Semester, 2005--Quiz Il Now, suppose we evaluate the following expressions in a fresh env ironment (define foo (set!-start 5)) (set! -careful foo 1 Att ached is a partial env ironment diagram associated with t his evaluat ion. You will notice that the frames are labeled El, etc, and the pro cedure ob jects are labeled Pl, etc. You should complete t his diagram to reflect the st ate of the env ironment after evaluat ion of bot h expressions in order, and then use the result s to answer the follow ing quest ions. Question 5: For each of t he following environments, indicate the value of it s enclosing environment. using one of GE, El, E2, E3 E4 or none or not show Enclosing env ironment El Question 6: For each of the follow ing variables, indicate the value to w hich it is bound, using a number, a symbol, a list of numbers or symbols, or one of P1, P2, P3, P4, P5 Variable Environment value to which bound set!-start GE et ! -careful GE val GE act ion val E3 current

￾       E ,& %% , - ) , <%%%   %) -! ￾  ￾  $ ￾   %& ) %   -  %% ,) )% -!  ,  ) ) %  0 ￾& !&  )  0 1%  0 $￾& !  %)  )%   F ) %  ) -  -  0) <%%%  &  ) % ) %%  %, ) , *%%! ￾    )  ) , -%&  ) -  % % -& %   "#$ #$ #$ #$ #     ! % - ￾ ' 5 6 ￾  %  )  ) , -0%&  ) -  ,))  % 0& %  0&  %.0&  %  0%  %.0%&    $ $ $  $ ! 0 -   ,)) 0 %G7%  %G7  - '   , 6  6 - 5 - 5  ￾

6.001, Spring Semester, 2005--Quiz Il ( d-tresn 0: Wh-z-is Ih-vuziubl-olVbound(w hul -nvizonm-n ) umd whul is ils vulu-? locution

￾       H ￾  & :) % ) -0   0 ?,) -B&  ,) % % -I 2 -2

6.001, Spring Semester, 2005--Quiz Il art Gu ol c EaO五i Here is a definit ion of a hig her order procedure for performing computat ions on trees(w here trees are represented as a list, w hose element s are eit her leaves(i.e, values such as numbers or sy mbols) or ot her trees). You may assume that leaf? is a predicate that returns true for any object that is ot a tree: (define (process-tree tr leaf-op combine init) if (null? tr) ini七 (if (leaf? tr) (leaf-op tr (combine (process-tree (car tr) leaf-op combine init) (process-tree (cdr tr) leaf ombine init))))) Below are a set of descript ions of operat ions on trees A: ret urns a new copy of the input tree B: ret urns a pointer to the original input tree C: sums the values of the leaves of the input tree D: sums the values of t he even-valued leaves of the input tree E: sums the values of the odd- valued leaves of the input tre reverses the top level of the input tree G: deep reverses the input tree(i.e. reverses each level of the tree count s the numbers of leaves in the input tree I: ret urns the value of the first leaf of the input tree J: flattens the input tree(i. e, ret urns a single list of all the elements of the tree in order K: none of the above

￾       J      %  =   ))     %  % ?,) %  % %  %& ,)% %  ) -% ?!!& -% %) % 0%  %.0%B  ) %B!  . %% )  %   ) %   . 0 1 ) %   2 ￾ ￾'     '   ￾ ￾   ￾ ￾  ￾ '  ￾  ￾'   ￾    '   ￾'   ￾    '   9,   %  %%  %  %2 2 %  , .  )   92 %    )    2 %% ) -%  ) -%  )   2 %% ) -%  ) -7- -%  )   2 %% ) -%  ) 7- -%  )   2 -%% )  -  )   2  -%% )   ?!! -%% ) -  ) B 2 % ) 0%  -%  )   2 % ) -  ) =%   )   K2 F% )   ?!!& %  % %   ) %  )   B 82   ) 0-

6. AAlCEpEi g HeF es(elCYAAre p air tt Fbr uoch ux? russibn bulbw, imunt ify thu musci? tibn in thu obbvu list thot bust mot chus its buhov ibr 1 M I(Ep 2 (process -eree er (lamb da (xl xl cons 1 (ll I M ICEp 5n 口□ 1 M I(Ep mn (process -eree er (lambda (xl 11+ol M D mn (process-eree er (lambda (xl xl (lambda (x yl (append y (lase xlll '(11 1 M ICEp (process -eree er lase append (11

￾       ￾"  ) <%% 0,& . ) %  ) 0- % ) 0% )% % 0)-! ￾  ' ￾'    ￾ ￾   ￾ ￾  ( ￾'     ( & ￾  ! ￾'    ￾ ￾ % ( & ￾   ￾'    ￾ ￾  ￾ ￾ ￾'' ￾  ￾ ￾   ￾'     '' ￾

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共21页,试读已结束,阅读完整版请下载
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有