<#54229#>(define-struct<#54229#> <#54230#>personnel<#54230#> <#54231#>(name<#54231#> <#54232#>address<#54232#> <#54233#>salary))<#54233#> <#71976#>;; A personnel record (<#69455#><#54234#>PR<#54234#><#69455#>) is a structure: <#71976#> <#71977#>;; <#69456#><#54235#>(make-personnel<#54235#> <#54236#>n<#54236#> <#54237#>a<#54237#> <#54238#>s)<#54238#><#69456#><#71977#> <#71978#>;; where <#69457#><#54239#>n<#54239#><#69457#> is a symbol, <#69458#><#54240#>a<#54240#><#69458#> is a string, and <#69459#><#54241#>s<#54241#><#69459#> is a number. <#71978#>A function that consumes such a record is based on the following template:
<#54249#>(d<#54249#><#54250#>efine<#54250#> <#54251#>(fun-for-personnel<#54251#> <#54252#>pr)<#54252#> <#54253#>...<#54253#> <#54254#>(personnel-name<#54254#> <#54255#>pr)<#54255#> <#54256#>...<#54256#> <#54257#>...<#54257#> <#54258#>(personnel-address<#54258#> <#54259#>pr)<#54259#> <#54260#>...<#54260#> <#54261#>...<#54261#> <#54262#>(personnel-salary<#54262#> <#54263#>pr)<#54263#> <#54264#>...)<#54264#>Consider a function for increasing the salary field:
<#71979#>;; <#69460#><#54272#>increase-salary<#54272#> <#54273#>:<#54273#> <#54274#>PR<#54274#> <#54275#>number<#54275#> <#54276#><#54276#><#54277#>-;SPMgt;<#54277#><#54278#><#54278#> <#54279#>void<#54279#><#69460#><#71979#> <#71980#>;; effect: to modify the salary field of <#69461#><#54280#>a-pr<#54280#><#69461#> by adding <#69462#><#54281#>a-raise<#54281#><#69462#><#71980#> <#54282#>(define<#54282#> <#54283#>(increase-salary<#54283#> <#54284#>a-pr<#54284#> <#54285#>a-raise)<#54285#> <#54286#>...)<#54286#>The contract specifies that the function consumes a <#69463#><#54290#>PR<#54290#><#69463#> and a number. The purpose statement is an effect statement, which explains how the argument of <#69464#><#54291#>increase-salary<#54291#><#69464#> is modified. Developing examples for <#69465#><#54292#>increase-salary<#54292#><#69465#> requires the techniques of section~#secdesignmemory#54293>
<#54299#>(l<#54299#><#54300#>ocal<#54300#> <#54301#>((define<#54301#> <#54302#>pr1<#54302#> <#54303#>(make-personnel<#54303#> <#54304#>'<#54304#><#54305#>Bob<#54305#> <#54306#>'<#54306#><#54307#>Pittsburgh<#54307#> <#54308#>70000)))<#54308#> <#54309#>(b<#54309#><#54310#>egin<#54310#> <#54311#>(increase-salary<#54311#> <#54312#>pr1<#54312#> <#54313#>10000)<#54313#> <#54314#>(=<#54314#> <#54315#>(personnel-salary<#54315#> <#54316#>pr1)<#54316#> <#54317#>80000)))<#54317#>The result of the expression is <#69467#><#54321#>true<#54321#><#69467#> if, and only if, <#69468#><#54322#>increase-salary<#54322#><#69468#> works properly for this example. We can now use the template and the example to define the function:
<#71981#>;; <#69469#><#54327#>increase-salary<#54327#> <#54328#>:<#54328#> <#54329#>PR<#54329#> <#54330#>number<#54330#> <#54331#><#54331#><#54332#>-;SPMgt;<#54332#><#54333#><#54333#> <#54334#>void<#54334#><#69469#><#71981#> <#71982#>;; effect: to modify the salary field of <#69470#><#54335#>a-pr<#54335#><#69470#> by adding in <#69471#><#54336#>a-raise<#54336#><#69471#><#71982#> <#54337#>(d<#54337#><#54338#>efine<#54338#> <#54339#>(increase-salary<#54339#> <#54340#>a-pr<#54340#> <#54341#>a-raise)<#54341#> <#54342#>(set-personnel-salary!<#54342#> <#54343#>a-pr<#54343#> <#54344#>(+<#54344#> <#54345#>(personnel-salary<#54345#> <#54346#>a-pr)<#54346#> <#54347#>a-raise)))<#54347#>As usual, the full definition uses only one of several subexpressions from the template, but the template reminds us of what information we can use: the arguments and their pieces, and what parts we can modify: the fields for which we have selectors.
<#54390#>(define-struct<#54390#> <#54391#>cheerleader<#54391#> <#54392#>(name<#54392#> <#54393#>dates))<#54393#> <#71983#>;; A <#69485#><#54394#>cheerleader<#54394#><#69485#> is a structure: <#71983#> <#71984#>;; <#69486#><#54395#>(make-cheerleader<#54395#> <#54396#>n<#54396#> <#54397#>d)<#54397#><#69486#><#71984#> <#71985#>;; where <#69487#><#54398#>n<#54398#><#69487#> is a symbol and <#69488#><#54399#>d<#54399#><#69488#> is a list of symbols. <#71985#>For example, <#69489#><#54403#>(make-cheerleader<#54403#>\ <#54404#>'<#54404#><#54405#>JoAnn<#54405#>\ <#54406#>'<#54406#><#54407#>(Carl<#54407#>\ <#54408#>Bob<#54408#>\ <#54409#>Dude<#54409#>\ <#54410#>Adam<#54410#>\ <#54411#>Emil))<#54411#><#69489#> is a valid <#69490#><#54412#>cheerleader<#54412#><#69490#> record. Develop an example that shows what it means to add <#69491#><#54413#>'<#54413#><#54414#>Frank<#54414#><#69491#> as a date.~ Solution<#69492#><#69492#> <#54420#>Exercise 41.2.5<#54420#>
<#54427#>(define-struct<#54427#> <#54428#>square<#54428#> <#54429#>(nw<#54429#> <#54430#>length<#54430#><#54431#>))<#54431#>The matching data definition specifies that the <#69494#><#54435#>nw<#54435#><#69494#> field is always a <#69495#><#54436#>posn<#54436#><#69495#> structure and that <#69496#><#54437#>length<#54437#><#69496#> is a number:
A <#69497#><#54439#>square<#54439#><#69497#> is a structure:We started working with <#69502#><#54447#>square<#54447#><#69502#>s in part~#partbasic#54448>
<#71986#><#69498#><#54440#>(make-square<#54440#>\ <#54441#>p<#54441#>\ <#54442#>s)<#54442#><#69498#><#71986#> where <#69499#><#54443#>p<#54443#><#69499#> is a <#69500#><#54444#>posn<#54444#><#69500#> and <#69501#><#54445#>s<#54445#><#69501#> is a number.
A <#69511#><#54464#>shape<#54464#><#69511#> is eitherSee exercise~#exmutstruct4#54470>
- a <#69512#><#54466#>circle<#54466#><#69512#>, or
- a <#69513#><#54467#>square<#54467#><#69513#>.
<#54480#>(d<#54480#><#54481#>efine<#54481#> <#54482#>(fun-for-shape<#54482#> <#54483#>a-shape)<#54483#> <#54484#>(c<#54484#><#54485#>ond<#54485#> <#54486#>[<#54486#><#54487#>(circle?<#54487#> <#54488#>a-shape)<#54488#> <#54489#>...<#54489#> <#54490#>(fun-for-circle<#54490#> <#54491#>a-shape)<#54491#> <#54492#>...]<#54492#> <#54493#>[<#54493#><#54494#>(square?<#54494#> <#54495#>a-shape)<#54495#> <#54496#>...<#54496#> <#54497#>(fun-for-square<#54497#> <#54498#>a-shape)<#54498#> <#54499#>...]<#54499#><#54500#>))<#54500#>Each <#69518#><#54504#>cond<#54504#><#69518#>-clause refers to a function with the same purpose for the matching kind of shape. So, suppose we wish to move a <#69519#><#54505#>shape<#54505#><#69519#> in the <#54506#>x<#54506#>-direction by a fixed number of pixels. In part~#partbasic#54507>
<#71987#>;; <#69522#><#54514#>move-shape!<#54514#> <#54515#>:<#54515#> <#54516#>shape<#54516#> <#54517#>number<#54517#> <#54518#><#54518#><#54519#>-;SPMgt;<#54519#><#54520#><#54520#> <#54521#>void<#54521#><#69522#><#71987#> <#71988#>;; effect: to move <#69523#><#54522#>a-shape<#54522#><#69523#> in the <#54523#>x<#54523#> direction by <#69524#><#54524#>delta<#54524#><#69524#> pixels<#71988#> <#54525#>(d<#54525#><#54526#>efine<#54526#> <#54527#>(move-shape!<#54527#> <#54528#>a-shape)<#54528#> <#54529#>(c<#54529#><#54530#>ond<#54530#> <#54531#>[<#54531#><#54532#>(circle?<#54532#> <#54533#>a-shape)<#54533#> <#54534#>(move-circle<#54534#> <#54535#>a-shape<#54535#> <#54536#>delta)]<#54536#> <#54537#>[<#54537#><#54538#>(square?<#54538#> <#54539#>a-shape)<#54539#> <#54540#>(move-square<#54540#> <#54541#>a-shape<#54541#> <#54542#>delta)]<#54542#><#54543#>))<#54543#>The functions <#69525#><#54547#>move-circle<#54547#><#69525#> and <#69526#><#54548#>move-square<#54548#><#69526#> are the subject of execise~#exmutstruct4#54549>
<#54566#>(define-struct<#54566#> <#54567#>cd<#54567#> <#54568#>(price<#54568#> <#54569#>title<#54569#> <#54570#>artist))<#54570#> <#54571#>(define-struct<#54571#> <#54572#>record<#54572#> <#54573#>(price<#54573#> <#54574#>antique<#54574#> <#54575#>title<#54575#> <#54576#>artist))<#54576#> <#54577#>(define-struct<#54577#> <#54578#>dvd<#54578#> <#54579#>(price<#54579#> <#54580#>title<#54580#> <#54581#>artist<#54581#> <#54582#>to-appear))<#54582#> <#54583#>(define-struct<#54583#> <#54584#>tape<#54584#> <#54585#>(price<#54585#> <#54586#>title<#54586#> <#54587#>artist))<#54587#>Provide a data definition for the class of <#69529#><#54591#>music item<#54591#><#69529#>s, which comprises <#69530#><#54592#>cd<#54592#><#69530#>s, <#69531#><#54593#>record<#54593#><#69531#>s, <#69532#><#54594#>dvd<#54594#><#69532#>s, and <#69533#><#54595#>tape<#54595#><#69533#>s. The price must be a number in each case. Develop the program <#69534#><#54596#>inflate!<#54596#><#69534#>, which consumes a <#69535#><#54597#>music-item<#54597#><#69535#> and a percentage. Its effect is to increase the price in the given structure according to the percentage.~ Solution<#69536#><#69536#> <#54603#>Exercise 41.2.8<#54603#>
<#54619#>(define-struct<#54619#> <#54620#>entry<#54620#> <#54621#>(name<#54621#> <#54622#>number))<#54622#>
An <#69541#><#54627#>entry<#54627#><#69541#> is a structure:
<#71989#><#69542#><#54628#>(make-entry<#54628#>\ <#54629#>n<#54629#>\ <#54630#>p)<#54630#><#69542#><#71989#> where <#69543#><#54631#>n<#54631#><#69543#> is a symbol and <#69544#><#54632#>p<#54632#><#69544#> is a number.
An <#69545#><#54635#>address book<#54635#><#69545#> isOnly the second one is self-referential, so we focus on the template for it:
- the empty list, <#69546#><#54637#>empty<#54637#><#69546#>, or
- <#69547#><#54638#>(cons<#54638#>\ <#54639#>an-e<#54639#>\ <#54640#>an-ab)<#54640#><#69547#> where <#69548#><#54641#>an-e<#54641#><#69548#> is an entry and <#69549#><#54642#>an-ab<#54642#><#69549#> is an address book.
<#71990#>;; <#69550#><#54649#>fun-for-ab<#54649#> <#54650#>:<#54650#> <#54651#>address-book<#54651#> <#54652#><#54652#><#54653#>-;SPMgt;<#54653#><#54654#><#54654#> <#54655#>XYZ<#54655#><#69550#><#71990#> <#54656#>(d<#54656#><#54657#>efine<#54657#> <#54658#>(fun-for-ab<#54658#> <#54659#>ab)<#54659#> <#54660#>(c<#54660#><#54661#>ond<#54661#> <#54662#>[<#54662#><#54663#>(empty?<#54663#> <#54664#>ab)<#54664#> <#54665#>...]<#54665#> <#54666#>[<#54666#><#54667#>else<#54667#> <#54668#>...<#54668#> <#54669#>(fun-for-entry<#54669#> <#54670#>(first<#54670#> <#54671#>ab))<#54671#> <#54672#>...<#54672#> <#54673#>(fun-for-ab<#54673#> <#54674#>(rest<#54674#> <#54675#>ab))<#54675#> <#54676#>...]<#54676#><#54677#>))<#54677#>If we needed an auxiliary function for processing an <#69551#><#54681#>entry<#54681#><#69551#>, we might also wish to write out the template for structure-processing functions. So suppose we want a function that updates an existing entry. The function consumes an <#69552#><#54682#>address-book<#54682#><#69552#>, a name, and a phone number. The first <#69553#><#54683#>entry<#54683#><#69553#> that contains the name is modified to contain the new phone number:
<#71991#>;; <#69554#><#54688#>change-number!<#54688#> <#54689#>:<#54689#> <#54690#>symbol<#54690#> <#54691#>number<#54691#> <#54692#>address-book<#54692#> <#54693#><#54693#><#54694#>-;SPMgt;<#54694#><#54695#><#54695#> <#54696#>void<#54696#><#69554#><#71991#> <#71992#>;; effect: to modify the first <#69555#><#54697#>entry<#54697#><#69555#> for <#69556#><#54698#>name<#54698#><#69556#> in <#69557#><#54699#>ab<#54699#><#69557#> so that its<#71992#> <#71993#>;; number field is <#69558#><#54700#>phone<#54700#><#69558#><#71993#> <#54701#>(define<#54701#> <#54702#>(change-number!<#54702#> <#54703#>name<#54703#> <#54704#>phone<#54704#> <#54705#>ab)<#54705#> <#54706#>...)<#54706#>It is justfied to develop this function with mutators because just as in reality, most of the address book stays the same while one entry is changed. Here is an example:
<#54714#>(d<#54714#><#54715#>efine<#54715#> <#54716#>ab<#54716#> <#54717#>(l<#54717#><#54718#>ist<#54718#> <#54719#>(make-entry<#54719#> <#54720#>'<#54720#><#54721#>Adam<#54721#> <#54722#>1)<#54722#> <#54723#>(make-entry<#54723#> <#54724#>'<#54724#><#54725#>Chris<#54725#> <#54726#>3)<#54726#> <#54727#>(make-entry<#54727#> <#54728#>'<#54728#><#54729#>Eve<#54729#> <#54730#>2)))<#54730#> <#54731#>(b<#54731#><#54732#>egin<#54732#> <#54733#>(change-number!<#54733#> <#54734#>'<#54734#><#54735#>Chris<#54735#> <#54736#>17<#54736#> <#54737#>ab)<#54737#> <#54738#>(=<#54738#> <#54739#>(entry-number<#54739#> <#54740#>(second<#54740#> <#54741#>ab))<#54741#> <#54742#>17))<#54742#>The definition introduces <#69559#><#54746#>ab<#54746#><#69559#>, an <#69560#><#54747#>address-book<#54747#><#69560#> with three items. The <#69561#><#54748#>begin<#54748#>-expression<#69561#> first changes <#69562#><#54749#>ab<#54749#><#69562#> by associating <#69563#><#54750#>'<#54750#><#54751#>Chris<#54751#><#69563#> with <#69564#><#54752#>17<#54752#><#69564#>; then it compares the phone number of the second item on <#69565#><#54753#>ab<#54753#><#69565#> with <#69566#><#54754#>17<#54754#><#69566#>. If <#69567#><#54755#>change-number!<#54755#><#69567#> functions properly, the result of the <#69568#><#54756#>begin<#54756#>-expression<#69568#> is <#69569#><#54757#>true<#54757#><#69569#>. An even better test would ensure that nothing else in <#69570#><#54758#>ab<#54758#><#69570#> changes. The next step is to develop the function definition, using the template and the examples. Let's consider each case separately:
<#54771#>(c<#54771#><#54772#>ond<#54772#> <#54773#>[<#54773#><#54774#>(symbol=?<#54774#> <#54775#>(entry-name<#54775#> <#54776#>(first<#54776#> <#54777#>ab))<#54777#> <#54778#>name)<#54778#> <#54779#>...]<#54779#> <#54780#>[<#54780#><#54781#>else<#54781#> <#54782#>...]<#54782#><#54783#>)<#54783#>In the first subcase, the function must modify the structure. In the second, <#69578#><#54787#>name<#54787#><#69578#> can occur only in <#69579#><#54788#>(rest<#54788#>\ <#54789#>ab)<#54789#><#69579#>, which means the function must mutate some <#69580#><#54790#>entry<#54790#><#69580#> in the rest of the list. Fortunately, the natural recursion accomplishes just that.
<#54796#>(d<#54796#><#54797#>efine<#54797#> <#54798#>(change-number!<#54798#> <#54799#>name<#54799#> <#54800#>phone<#54800#> <#54801#>ab)<#54801#> <#54802#>(c<#54802#><#54803#>ond<#54803#> <#54804#>[<#54804#><#54805#>(empty?<#54805#> <#54806#>ab)<#54806#> <#54807#>(error<#54807#> <#54808#>'<#54808#><#54809#>change-number!<#54809#> <#54810#>``name<#54810#> <#54811#>not<#54811#> <#54812#>in<#54812#> <#54813#>list'')]<#54813#> <#54814#>[<#54814#><#54815#>else<#54815#> <#54816#>(c<#54816#><#54817#>ond<#54817#> <#54818#>[<#54818#><#54819#>(symbol=?<#54819#> <#54820#>(entry-name<#54820#> <#54821#>(first<#54821#> <#54822#>ab))<#54822#> <#54823#>name)<#54823#> <#54824#>(set-entry-number!<#54824#> <#54825#>(first<#54825#> <#54826#>ab)<#54826#> <#54827#>phone)]<#54827#> <#54828#>[<#54828#><#54829#>else<#54829#> <#54830#>(change-number!<#54830#> <#54831#>name<#54831#> <#54832#>phone<#54832#> <#54833#>(rest<#54833#> <#54834#>ab))]<#54834#><#54835#>)]<#54835#><#54836#>))<#54836#>The only unique aspect of this function is that it uses a structure mutator in one of the cases. Otherwise it has the familiar recursive shape: a <#69581#><#54840#>cond<#54840#><#69581#> with two clauses and a natural recursion. It is especially instructive to compare the function with <#69582#><#54841#>contains-doll?<#54841#><#69582#> from section~#seclistsprocess#54842>
<#54907#>(define-struct<#54907#> <#54908#>parent<#54908#> <#54909#>(children<#54909#> <#54910#>name<#54910#> <#54911#>date<#54911#> <#54912#>eyes<#54912#> <#54913#>no-descendants))<#54913#>The last field in a <#69605#><#54917#>parent<#54917#><#69605#> structure is originally <#69606#><#54918#>0<#54918#><#69606#>. The function <#69607#><#54919#>ft-descendants<#54919#><#69607#> traverses the tree and modifies these slots so that they contain the total number of descendants of the corresponding family member. Its result is the number of total descendants of the given tree.~ Solution<#69608#><#69608#>
<#71994#>;; <#69614#><#54937#>call-status<#54937#> <#54938#>:<#54938#> <#54939#>(vectorof<#54939#> <#54940#>boolean)<#54940#><#69614#><#71994#> <#54941#>;; to keep track of the floors from which calls have been issued <#54941#> <#54942#>(define<#54942#> <#54943#>call-status<#54943#> <#54944#>(vector<#54944#> <#54945#>true<#54945#> <#54946#>true<#54946#> <#54947#>true<#54947#> <#54948#>false<#54948#> <#54949#>true<#54949#> <#54950#>true<#54950#> <#54951#>true<#54951#> <#54952#>false<#54952#><#54953#>))<#54953#> <#71995#>;; <#69615#><#54954#>reset<#54954#> <#54955#>:<#54955#> <#54956#><#54956#><#54957#>-;SPMgt;<#54957#><#54958#><#54958#> <#54959#>true<#54959#><#69615#><#71995#> <#71996#>;; effect: to set all fields in <#69616#><#54960#>call-status<#54960#><#69616#> to <#69617#><#54961#>false<#54961#><#69617#><#71996#> <#54962#>(define<#54962#> <#54963#>(reset)<#54963#> <#54964#>...)<#54964#>The first definition specifies <#69618#><#54968#>call-status<#54968#><#69618#> as a state variable but of course we use each slot in the vector as a state value not the entire variable. The second part consists of three pieces: a contract, an effect statement, and a header for the function <#69619#><#54969#>reset<#54969#><#69619#>, which implements the informally specified service. While it is possible to implement the service as
<#54974#>(d<#54974#><#54975#>efine<#54975#> <#54976#>(reset)<#54976#> <#54977#>(s<#54977#><#54978#>et!<#54978#> <#54979#>call-status<#54979#> <#54980#>(build-vector<#54980#> <#54981#>(vector-length<#54981#> <#54982#>call-status)<#54982#> <#54983#>(lambda<#54983#> <#54984#>(i)<#54984#> <#54985#>false))))<#54985#>this trivial solution is clearly not what we want. Instead, we want a function that modifies each field of the vector. Following the suggestions of intermezzo~5, we develop an auxiliary function with the following template:
<#71997#>;; <#69620#><#54993#>reset-aux<#54993#> <#54994#>:<#54994#> <#54995#>(vectorof<#54995#> <#54996#>boolean)<#54996#> <#54997#>N<#54997#> <#54998#><#54998#><#54999#>-;SPMgt;<#54999#><#55000#><#55000#> <#55001#>void<#55001#><#69620#><#71997#> <#71998#>;; effect: to set the fields of <#69621#><#55002#>v<#55002#><#69621#> with index in [<#69622#><#55003#>0<#55003#><#69622#>, <#69623#><#55004#>i<#55004#><#69623#>) to <#69624#><#55005#>false<#55005#><#69624#><#71998#> <#55006#>(d<#55006#><#55007#>efine<#55007#> <#55008#>(reset-aux<#55008#> <#55009#>v<#55009#> <#55010#>i)<#55010#> <#55011#>(c<#55011#><#55012#>ond<#55012#> <#55013#>[<#55013#><#55014#>(zero?<#55014#> <#55015#>i)<#55015#> <#55016#>...]<#55016#> <#55017#>[<#55017#><#55018#>else<#55018#> <#55019#>...<#55019#> <#55020#>(reset-aux<#55020#> <#55021#>v<#55021#> <#55022#>(sub1<#55022#> <#55023#>i))<#55023#> <#55024#>...]<#55024#><#55025#>))<#55025#>That is, the auxiliary function not only consumes the vector but also an interval bound. The shape of the template is based on the data definition of the latter. The effect statement suggests the following examples:
<#71999#>;; <#69654#><#55085#>call-status<#55085#> <#55086#>:<#55086#> <#55087#>(vectorof<#55087#> <#55088#>boolean)<#55088#><#69654#><#71999#> <#55089#>;; to keep track of the floors from which calls have been issued <#55089#> <#55090#>(define<#55090#> <#55091#>call-status<#55091#> <#55092#>(vector<#55092#> <#55093#>true<#55093#> <#55094#>true<#55094#> <#55095#>true<#55095#> <#55096#>false<#55096#> <#55097#>true<#55097#> <#55098#>true<#55098#> <#55099#>true<#55099#> <#55100#>false<#55100#><#55101#>))<#55101#> <#72000#>;; <#69655#><#55102#>reset<#55102#> <#55103#>:<#55103#> <#55104#><#55104#><#55105#>-;SPMgt;<#55105#><#55106#><#55106#> <#55107#>true<#55107#><#69655#><#72000#> <#72001#>;; effect: to set all fields in <#69656#><#55108#>call-status<#55108#><#69656#> to <#69657#><#55109#>false<#55109#><#69657#><#72001#> <#55110#>(d<#55110#><#55111#>efine<#55111#> <#55112#>(reset)<#55112#> <#55113#>(reset-aux<#55113#> <#55114#>call-status<#55114#> <#55115#>(vector-length<#55115#> <#55116#>call-status)))<#55116#> <#72002#>;; <#69658#><#55117#>reset-aux<#55117#> <#55118#>:<#55118#> <#55119#>(vectorof<#55119#> <#55120#>boolean)<#55120#> <#55121#>N<#55121#> <#55122#><#55122#><#55123#>-;SPMgt;<#55123#><#55124#><#55124#> <#55125#>void<#55125#><#69658#><#72002#> <#72003#>;; effect: to set the fields of <#69659#><#55126#>v<#55126#><#69659#> with index in [<#69660#><#55127#>0<#55127#><#69660#>, <#69661#><#55128#>i<#55128#><#69661#>) to <#69662#><#55129#>false<#55129#><#69662#><#72003#> <#55130#>(d<#55130#><#55131#>efine<#55131#> <#55132#>(reset-aux<#55132#> <#55133#>v<#55133#> <#55134#>i)<#55134#> <#55135#>(c<#55135#><#55136#>ond<#55136#> <#55137#>[<#55137#><#55138#>(zero?<#55138#> <#55139#>i)<#55139#> <#55140#>(<#55140#><#55141#>void<#55141#><#55142#>)]<#55142#> <#55143#>[<#55143#><#55144#>else<#55144#> <#55145#>(b<#55145#><#55146#>egin<#55146#> <#55147#>(vector-set!<#55147#> <#55148#>v<#55148#> <#55149#>(sub1<#55149#> <#55150#>i)<#55150#> <#55151#>false)<#55151#> <#55152#>(reset-aux<#55152#> <#55153#>v<#55153#> <#55154#>(sub1<#55154#> <#55155#>i)))]<#55155#><#55156#>))<#55156#><#55160#>Figure: Resetting call-buttons for an elevator<#55160#>
<#72004#>;; <#69666#><#55179#>reset-interval<#55179#> <#55180#>:<#55180#> <#55181#>N<#55181#> <#55182#>N<#55182#> <#55183#><#55183#><#55184#>-;SPMgt;<#55184#><#55185#><#55185#> <#55186#>(<#55186#><#55187#>void<#55187#><#55188#>)<#55188#><#69666#><#72004#> <#72005#>;; effect: to set all fields in [<#69667#><#55189#>from<#55189#><#69667#>, <#69668#><#55190#>to<#55190#><#69668#>] to <#69669#><#55191#>false<#55191#><#69669#><#72005#> <#72006#>;; assume: <#69670#><#55192#>(;SPMlt;=<#55192#> <#55193#>from<#55193#> <#55194#>to)<#55194#><#69670#> holds <#72006#> <#55195#>(define<#55195#> <#55196#>(reset-interval<#55196#> <#55197#>from<#55197#> <#55198#>to)<#55198#> <#55199#>...)<#55199#>Use <#69671#><#55203#>reset-interval<#55203#><#69671#> to define <#69672#><#55204#>reset<#55204#><#69672#>.~ Solution<#69673#><#69673#> <#55210#>Exercise 41.2.16<#55210#>
<#72007#>;; <#69675#><#55217#>move!<#55217#> <#55218#>:<#55218#> <#55219#>(vectorof<#55219#> <#55220#>number)<#55220#> <#55221#>(vectorof<#55221#> <#55222#>number)<#55222#> <#55223#><#55223#><#55224#>-;SPMgt;<#55224#><#55225#><#55225#> <#55226#>void<#55226#><#69675#><#72007#> <#72008#>;; effect: to add the fields of <#69676#><#55227#>v<#55227#><#69676#> to the corresponding fields of <#69677#><#55228#>pos<#55228#><#69677#> <#72008#> <#55229#>(define<#55229#> <#55230#>(move!<#55230#> <#55231#>pos<#55231#> <#55232#>v)<#55232#> <#55233#>...)<#55233#>Justify why the use of a vector-modifying function is appropriate to model the movement of an object.~ Solution<#69678#><#69678#> <#55242#>Exercise 41.2.17<#55242#>
<#72009#>;; <#69690#><#55262#>vec-for-all<#55262#> <#55263#>:<#55263#> <#55264#>(<#55264#><#55265#>N<#55265#> <#55266#>X<#55266#> <#55267#><#55267#><#55268#>-;SPMgt;<#55268#><#55269#><#55269#> <#55270#>void)<#55270#> <#55271#>(vectorof<#55271#> <#55272#>X)<#55272#> <#55273#><#55273#><#55274#>-;SPMgt;<#55274#><#55275#><#55275#> <#55276#>void<#55276#><#69690#><#72009#> <#72010#>;; effect: to apply <#69691#><#55277#>f<#55277#><#69691#> to all indices and values in <#69692#><#55278#>vec<#55278#><#69692#><#72010#> <#55279#>;; equation: <#55279#> <#72011#>;; <#69693#><#55280#>(vec-for-all<#55280#> <#55281#>f<#55281#> <#55282#>(vector<#55282#> <#55283#>v-0<#55283#> <#55284#>...<#55284#> <#55285#>v-N))<#55285#><#69693#> <#72011#> <#55286#>;; = <#55286#> <#72012#>;; <#69694#><#55287#>(begin<#55287#> <#55288#>(f<#55288#> <#55289#>N<#55289#> <#55290#>v-N)<#55290#> <#55291#>...<#55291#> <#55292#>(f<#55292#> <#55293#>0<#55293#> <#55294#>v-0)<#55294#> <#55295#>(<#55295#><#55296#>void<#55296#><#55297#>))<#55297#><#69694#><#72012#> <#55298#>(define<#55298#> <#55299#>(vec-for-all<#55299#> <#55300#>f<#55300#> <#55301#>vec)<#55301#> <#55302#>...)<#55302#>Use <#69695#><#55306#>vec-for-all<#55306#><#69695#> to define <#69696#><#55307#>vector*!<#55307#><#69696#>, which consumes a number <#69697#><#55308#>s<#55308#><#69697#> and a vector of numbers and modifies the vector by multiplying each field's value with~<#69698#><#55309#>s<#55309#><#69698#>.~ Solution<#69699#><#69699#>
<#72013#>;; <#69700#><#55322#>count-vowels<#55322#> <#55323#>:<#55323#> <#55324#>(listof<#55324#> <#55325#>letter)<#55325#> <#69700#><#72013#> <#72014#>;; <#69701#> <#55326#><#55326#><#55327#>-;SPMgt;<#55327#><#55328#><#55328#> <#55329#>(vector<#55329#> <#55330#>number<#55330#> <#55331#>number<#55331#> <#55332#>number<#55332#> <#55333#>number<#55333#> <#55334#>number)<#55334#><#69701#><#72014#> <#72015#>;; where a <#69702#><#55335#>letter<#55335#><#69702#> is a symbol in <#69703#><#55336#>'<#55336#><#55337#>a<#55337#> <#55338#>...<#55338#> <#55339#>'<#55339#><#55340#>z<#55340#><#69703#><#72015#> <#72016#>;; to determine how many times the five vowels occur in <#69704#><#55341#>chars<#55341#><#69704#><#72016#> <#55342#>;; the resulting vector lists the counts in the lexicographic order<#55342#> <#55343#>(define<#55343#> <#55344#>(count-vowels<#55344#> <#55345#>chars)<#55345#> <#55346#>...)<#55346#>The choice of vector as a result is appropriate because the function must combine five values into one and each of the values is equally interesting. Using the purpose statement, we can also come up with examples:
<#55354#>(count-vowels<#55354#> <#55355#>'<#55355#><#55356#>(a<#55356#> <#55357#>b<#55357#> <#55358#>c<#55358#> <#55359#>d<#55359#> <#55360#>e<#55360#> <#55361#>f<#55361#> <#55362#>g<#55362#> <#55363#>h<#55363#> <#55364#>i))<#55364#> <#55365#>=<#55365#> <#55366#>(vector<#55366#> <#55367#>1<#55367#> <#55368#>1<#55368#> <#55369#>1<#55369#> <#55370#>0<#55370#> <#55371#>0)<#55371#> <#55372#>(count-vowels<#55372#> <#55373#>'<#55373#><#55374#>(a<#55374#> <#55375#>a<#55375#> <#55376#>i<#55376#> <#55377#>u<#55377#> <#55378#>u))<#55378#> <#55379#>=<#55379#> <#55380#>(vector<#55380#> <#55381#>2<#55381#> <#55382#>0<#55382#> <#55383#>1<#55383#> <#55384#>0<#55384#> <#55385#>2)<#55385#>Given that the input is a list, the natural choice for the template is that for a list-processing function:
<#55394#>(d<#55394#><#55395#>efine<#55395#> <#55396#>(count-vowels<#55396#> <#55397#>chars)<#55397#> <#55398#>(c<#55398#><#55399#>ond<#55399#> <#55400#>[<#55400#><#55401#>(empty?<#55401#> <#55402#>chars)<#55402#> <#55403#>...]<#55403#> <#55404#>[<#55404#><#55405#>else<#55405#> <#55406#>...<#55406#> <#55407#>(first<#55407#> <#55408#>chars)<#55408#> <#55409#>...<#55409#> <#55410#>(count-vowels<#55410#> <#55411#>(rest<#55411#> <#55412#>chars))<#55412#> <#55413#>...<#55413#> <#55414#>]<#55414#><#55415#>))<#55415#>
<#72017#>;; <#69705#><#55423#>count-vowels<#55423#> <#55424#>:<#55424#> <#55425#>(listof<#55425#> <#55426#>letter)<#55426#> <#69705#><#72017#> <#72018#>;; <#69706#> <#55427#><#55427#><#55428#>-;SPMgt;<#55428#><#55429#><#55429#> <#55430#>(vector<#55430#> <#55431#>number<#55431#> <#55432#>number<#55432#> <#55433#>number<#55433#> <#55434#>number<#55434#> <#55435#>number)<#55435#><#69706#><#72018#> <#72019#>;; where a <#69707#><#55436#>letter<#55436#><#69707#> is a symbol in <#69708#><#55437#>'<#55437#><#55438#>a<#55438#> <#55439#>...<#55439#> <#55440#>'<#55440#><#55441#>z<#55441#><#69708#><#72019#> <#72020#>;; to determine how many times the five vowels occur in <#69709#><#55442#>chars<#55442#><#69709#><#72020#> <#55443#>;; the resulting vector lists the counts in the lexicographic order<#55443#> <#55444#>(d<#55444#><#55445#>efine<#55445#> <#55446#>(count-vowels<#55446#> <#55447#>chars)<#55447#> <#55448#>(c<#55448#><#55449#>ond<#55449#> <#55450#>[<#55450#><#55451#>(empty?<#55451#> <#55452#>chars)<#55452#> <#55453#>(vector<#55453#> <#55454#>0<#55454#> <#55455#>0<#55455#> <#55456#>0<#55456#> <#55457#>0<#55457#> <#55458#>0)]<#55458#> <#55459#>[<#55459#><#55460#>else<#55460#> <#55461#>(l<#55461#><#55462#>ocal<#55462#> <#55463#>((define<#55463#> <#55464#>count-rest<#55464#> <#55465#>(count-vowels<#55465#> <#55466#>(rest<#55466#> <#55467#>chars))))<#55467#> <#55468#>(b<#55468#><#55469#>egin<#55469#> <#55470#>(count-a-vowel<#55470#> <#55471#>(first<#55471#> <#55472#>chars)<#55472#> <#55473#>count-rest)<#55473#> <#55474#>count-rest))]<#55474#><#55475#>))<#55475#> <#72021#>;; <#69710#><#55476#>count-a-vowel<#55476#> <#55477#>:<#55477#> <#55478#>letter<#55478#> <#55479#><#55479#><#55480#>-;SPMgt;<#55480#><#55481#><#55481#> <#55482#>void<#55482#><#69710#><#72021#> <#72022#>;; effect: to modify <#69711#><#55483#>counts<#55483#><#69711#> at the appropriate place if <#69712#><#55484#>l<#55484#><#69712#> is a vowel, <#72022#> <#55485#>;; none otherwise<#55485#> <#55486#>(d<#55486#><#55487#>efine<#55487#> <#55488#>(count-a-vowel<#55488#> <#55489#>counts<#55489#> <#55490#>l)<#55490#> <#55491#>...)<#55491#><#55495#>Figure: Counting vowels<#55495#>
<#72023#>;; <#69720#><#55512#>count-a-vowel<#55512#> <#55513#>:<#55513#> <#55514#>letter<#55514#> <#55515#><#55515#><#55516#>-;SPMgt;<#55516#><#55517#><#55517#> <#55518#>void<#55518#><#69720#><#72023#> <#72024#>;; effect: to modify <#69721#><#55519#>counts<#55519#><#69721#> at the appropriate place if <#69722#><#55520#>c<#55520#><#69722#> is a vowel, <#72024#> <#55521#>;; none otherwise<#55521#> <#55522#>(define<#55522#> <#55523#>(count-a-vowel<#55523#> <#55524#>counts<#55524#> <#55525#>c)<#55525#> <#55526#>...)<#55526#>In other words, the second clause first counts the vowels in the rest of the list. This computation is guaranteed to yield a vector according to the purpose statement. Let's call this vector <#69723#><#55530#>counts<#55530#><#69723#>. Then, it uses <#69724#><#55531#>count-a-vowel<#55531#><#69724#> to increase the appropriate field in <#69725#><#55532#>counts<#55532#><#69725#>, if any. The result is <#69726#><#55533#>counts<#55533#><#69726#>, after the first letter on the list has been counted.
<#55557#>(d<#55557#><#55558#>efine<#55558#> <#55559#>(count-vowels-bv<#55559#> <#55560#>chars)<#55560#> <#55561#>(l<#55561#><#55562#>ocal<#55562#> <#55563#>((d<#55563#><#55564#>efine<#55564#> <#55565#>(count-vowel<#55565#> <#55566#>x<#55566#> <#55567#>chars)<#55567#> <#55568#>(c<#55568#><#55569#>ond<#55569#> <#55570#>[<#55570#><#55571#>(empty?<#55571#> <#55572#>chars)<#55572#> <#55573#>0]<#55573#> <#55574#>[<#55574#><#55575#>else<#55575#> <#55576#>(c<#55576#><#55577#>ond<#55577#> <#55578#>[<#55578#><#55579#>(symbol=?<#55579#> <#55580#>x<#55580#> <#55581#>(first<#55581#> <#55582#>chars))<#55582#> <#55583#>(+<#55583#> <#55584#>(count-vowel<#55584#> <#55585#>x<#55585#> <#55586#>(rest<#55586#> <#55587#>chars))<#55587#> <#55588#>1)]<#55588#> <#55589#>[<#55589#><#55590#>else<#55590#> <#55591#>(count-vowel<#55591#> <#55592#>x<#55592#> <#55593#>(rest<#55593#> <#55594#>chars))]<#55594#><#55595#>)]<#55595#><#55596#>)))<#55596#> <#55597#>(build-vector<#55597#> <#55598#>5<#55598#> <#55599#>(l<#55599#><#55600#>ambda<#55600#> <#55601#>(i)<#55601#> <#55602#>(c<#55602#><#55603#>ond<#55603#> <#55604#>[<#55604#><#55605#>(=<#55605#> <#55606#>i<#55606#> <#55607#>0)<#55607#> <#55608#>(count-vowel<#55608#> <#55609#>'<#55609#><#55610#>a<#55610#> <#55611#>chars)]<#55611#> <#55612#>[<#55612#><#55613#>(=<#55613#> <#55614#>i<#55614#> <#55615#>1)<#55615#> <#55616#>(count-vowel<#55616#> <#55617#>'<#55617#><#55618#>e<#55618#> <#55619#>chars)]<#55619#> <#55620#>[<#55620#><#55621#>(=<#55621#> <#55622#>i<#55622#> <#55623#>2)<#55623#> <#55624#>(count-vowel<#55624#> <#55625#>'<#55625#><#55626#>i<#55626#> <#55627#>chars)]<#55627#> <#55628#>[<#55628#><#55629#>(=<#55629#> <#55630#>i<#55630#> <#55631#>3)<#55631#> <#55632#>(count-vowel<#55632#> <#55633#>'<#55633#><#55634#>o<#55634#> <#55635#>chars)]<#55635#> <#55636#>[<#55636#><#55637#>(=<#55637#> <#55638#>i<#55638#> <#55639#>4)<#55639#> <#55640#>(count-vowel<#55640#> <#55641#>'<#55641#><#55642#>u<#55642#> <#55643#>chars)]<#55643#><#55644#>)))))<#55644#><#55648#>Figure: Another way of counting vowels<#55648#>