Structural Design Recipes and Mutation, Part 1

Surprisingly programming with mutators does not require any new design recipes---as long as the mutated fields always contain atomic values. Our receipes work perfectly fine. While the design of non-mutating programs requires the combination of values, programming with mutators requires the combination of effects. Hence, the key is to add a well-formulated effect statement to a function's contract and to make up examples that illustrate the effects. We practiced both of these activities for <#69454#><#54223#>set!<#54223#><#69454#> expressions already in section~#secdesignmemory#54224>. In this section we learn to adapt the design recipes and effect statements to structure and vector mutations. To do that, we consider a short series of examples. Each illustrates how an applicable design recipe helps with the design of structure-modifying or vector-modifying functions. The first example concerns the mutation of plain structures. Suppose we are given a structure and a data definition for personnel records:
<#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>. Specifcially, we must be able to compare the before and after state of some <#69466#><#54294#>PR<#54294#><#69466#> structure:
<#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.
<#54353#>Exercise 41.2.1<#54353#> Make up examples for <#69472#><#54355#>increase-salary<#54355#><#69472#> and test the function. Formulate the tests as boolean-valued expressions.~ external Solution<#69473#><#69473#> <#54361#>Exercise 41.2.2<#54361#> Adapt <#69474#><#54363#>increase-salary<#54363#><#69474#> such that it accepts only values for <#69475#><#54364#>a-raise<#54364#><#69475#> between 3 and 7 of the salary. It calls <#69476#><#54365#>error<#54365#><#69476#> otherwise.~ external Solution<#69477#><#69477#> <#54371#>Exercise 41.2.3<#54371#> Develop <#69478#><#54373#>increase-percentage<#54373#><#69478#>. The function consumes a <#69479#><#54374#>PR<#54374#><#69479#> and a percentage between 3 and 7. It increases the value in the salary field of the <#69480#><#54375#>PR<#54375#><#69480#> by the lesser of the percentage increase or <#69481#><#54376#>7000<#54376#><#69481#>.~ external Solution<#69482#><#69482#> <#54382#>Exercise 41.2.4<#54382#> Develop the function <#69483#><#54384#>new-date<#54384#><#69483#>. It consumes a <#69484#><#54385#>cheerleader<#54385#><#69484#> record and adds a date to the beginning of a list. Here are the relevant definitions:
<#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.~ external Solution<#69492#><#69492#> <#54420#>Exercise 41.2.5<#54420#> Recall the structure definitions for <#69493#><#54422#>square<#54422#><#69493#>s:
<#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:

<#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.

We started working with <#69502#><#54447#>square<#54447#><#69502#>s in part~#partbasic#54448>. Develop the function <#69503#><#54449#>move-square!<#54449#><#69503#>. It consume a square, called <#69504#><#54450#>sq<#54450#><#69504#>, and a number, called <#69505#><#54451#>delta<#54451#><#69505#>. It modifies <#69506#><#54452#>sq<#54452#><#69506#> by adding <#69507#><#54453#>delta<#54453#><#69507#> to its x-coordinate. Look up the structure and data definition for circles and develop the function <#69508#><#54454#>move-circle<#54454#><#69508#>, which is analogous to <#69509#><#54455#>move-square<#54455#><#69509#>.~ external Solution<#69510#><#69510#>
The second example recalls the design recipe for functions that work on unions of classes. One of our first examples of this kind concerned the class of geometric shapes. Here is a data definition that merges squares and circles:
A <#69511#><#54464#>shape<#54464#><#69511#> is either
  1. a <#69512#><#54466#>circle<#54466#><#69512#>, or
  2. a <#69513#><#54467#>square<#54467#><#69513#>.
See exercise~#exmutstruct4#54470> or part~#partbasic#54471> for the definitions of <#69514#><#54472#>circle<#54472#><#69514#> and <#69515#><#54473#>square<#54473#><#69515#>. Following our recipe, a template for <#69516#><#54474#>shape<#54474#><#69516#>-processing functions consists of a two-clause <#69517#><#54475#>cond<#54475#>-expression<#69517#>:
<#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>, we created a new structure for this puropose. Now we can use the mutators for <#69520#><#54508#>circle<#54508#><#69520#> and <#69521#><#54509#>square<#54509#><#69521#> structures instead, that is, the function can have an effect:
<#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> because they consume and affect plain structures.
<#54552#>Exercise 41.2.6<#54552#> Make up examples for <#69527#><#54554#>move-shape!<#54554#><#69527#> and test the function. Formulate the tests as boolean-valued expressions!~ external Solution<#69528#><#69528#> <#54560#>Exercise 41.2.7<#54560#> The following structure definitions are to represent items that a music store sells:
<#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.~ external Solution<#69536#><#69536#> <#54603#>Exercise 41.2.8<#54603#> Develop a program that keeps track of the feeding of zoo animals. Our zoo has three kinds of animals: elephants, monkeys, and spiders. Each animal has a name and two feeding times per day: morning and evening. Initially a structure that represents an animal (structure) contains <#69537#><#54605#>false<#54605#><#69537#> in the fields for feeding times. The program <#69538#><#54606#>feed-animal<#54606#><#69538#> should consume a structure that represents an animal and the name of a feeding time. It should switch the corresponding field in the animal structure to <#69539#><#54607#>true<#54607#><#69539#>.~ external Solution<#69540#><#69540#>
The next two examples are about mutations when the underlying data definitions involve self-references. Self-references are needed if we wish to deal with data that has no size limit. Lists were the first class of such data we encountered and natural numbers the second one. Let's first take a look at mutation of lists of structures, using the running data example of this part: the address book. An address book is a list of entries; for completeness, here are the structure and data definitions:
<#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#> is
  1. the empty list, <#69546#><#54637#>empty<#54637#><#69546#>, or
  2. <#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.
Only the second one is self-referential, so we focus on the template for it:
<#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:
  1. If <#69571#><#54760#>ab<#54760#><#69571#> is empty, <#69572#><#54761#>name<#54761#><#69572#> doesn't occur in it. Unfortunately, the purpose statement doesn't specify what the function should compute in this case, and there is indeed nothing sensible the function can do. To be safe, we use <#69573#><#54762#>error<#54762#><#69573#> to signal that no matching entry was found.
  2. If <#69574#><#54763#>ab<#54763#><#69574#> contains a first <#69575#><#54764#>entry<#54764#><#69575#>, it might or might not contain <#69576#><#54765#>name<#54765#><#69576#>. To find out, the function must distinguish the two cases with a <#69577#><#54766#>cond<#54766#>-expression<#69577#>:
    <#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.
Putting everything together, we get the following definition:
<#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> and <#69583#><#54843#>contains?<#54843#><#69583#> from exercise~#excontainsp#54844>.
<#54847#>Exercise 41.2.9<#54847#> Define <#69584#><#54849#>test-change-number<#54849#><#69584#>. The function consumes a name, a phone number, and an address book. It uses <#69585#><#54850#>change-number!<#54850#><#69585#> to update the address book, and then ensures that it was changed properly. If so, it produces <#69586#><#54851#>true<#54851#><#69586#>; if not, it produces an error message. Use this new function to test <#69587#><#54852#>change-number!<#54852#><#69587#> with at least three different examples.~ external Solution<#69588#><#69588#> <#54858#>Exercise 41.2.10<#54858#> Develop <#69589#><#54860#>move-squares<#54860#><#69589#>. It consumes a list of <#69590#><#54861#>square<#54861#><#69590#>s, as defined above, and a number <#69591#><#54862#>delta<#54862#><#69591#>. The function modifies each on the list by adding <#69592#><#54863#>delta<#54863#><#69592#> to the <#54864#>x<#54864#>-component of its position.~ external Solution<#69593#><#69593#> <#54870#>Exercise 41.2.11<#54870#> Develop the function <#69594#><#54872#>all-fed<#54872#><#69594#>. It consumes a list of <#69595#><#54873#>animal<#54873#><#69595#>s, as defined in exercise~#exmutrec1#54874>, and modifies them so that their field for morning feedings is switched to <#69596#><#54875#>true<#54875#><#69596#>.~ external Solution<#69597#><#69597#> <#54881#>Exercise 41.2.12<#54881#> Develop the function <#69598#><#54883#>for-all<#54883#><#69598#>, which abstracts <#69599#><#54884#>move-squares<#54884#><#69599#> and <#69600#><#54885#>all-fed<#54885#><#69600#> from exercises~#exrec1#54886> and~#exrec3#54887>. It consumes two values: a function that consumes structures and produces <#69601#><#54888#>(<#54888#><#54889#>void<#54889#><#54890#>)<#54890#><#69601#>; and a list of structures. Its result is <#69602#><#54891#>(<#54891#><#54892#>void<#54892#><#54893#>)<#54893#><#69602#>.~ external Solution<#69603#><#69603#> <#54899#>Exercise 41.2.13<#54899#> Develop the function <#69604#><#54901#>ft-descendants<#54901#><#69604#>. It consumes a descendant family tree (see section~#secmutrefdd#54902>) based on the following structure definition:
<#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.~ external Solution<#69608#><#69608#>
Natural numbers make up another class of values that requires a self-referential descriptions. Recursion on natural numbers <#54927#>per se<#54927#> isn't useful in conjunction with mutation, but recursion on natural numbers as indices into vectors is useful when a problem's data representation involves vectors. Let's start with a snipet of an elevator control program. An elevator control program must know at which floor people have pressed the call buttons. We assume that the elevator hardware can mutate some status vector of booleans. That is, we assume that the program contains a vector, call it <#69609#><#54928#>call-status<#54928#><#69609#>, and that a field in <#69610#><#54929#>call-status<#54929#><#69610#> is <#69611#><#54930#>true<#54930#><#69611#> if someone has pushed the call button at the corresponding floor. One important elevator operation is to <#69612#><#54931#>reset<#54931#><#69612#> all the buttons. For example, an operator may have to restart the elevator after it has been out of service for a while. We start the development of <#69613#><#54932#>reset<#54932#><#69613#> by re-stating the known facts in a Scheme outline:
<#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:
  1. <#69625#><#55030#>(reset-aux<#55030#>\ <#55031#>call-status<#55031#>\ <#55032#>0)<#55032#><#69625#> leaves <#69626#><#55033#>call-status<#55033#><#69626#> unchanged, because the purpose statement says to change all indices in [<#69627#><#55034#>0<#55034#><#69627#>,<#69628#><#55035#>0<#55035#><#69628#>) and there are none;
  2. <#69629#><#55036#>(reset-aux<#55036#>\ <#55037#>1)<#55037#><#69629#> changes <#69630#><#55038#>call-status<#55038#><#69630#> so that <#69631#><#55039#>(vector-ref<#55039#>\ <#55040#>call-status<#55040#>\ <#55041#>0)<#55041#><#69631#> is <#69632#><#55042#>false<#55042#><#69632#>, because <#69633#><#55043#>0<#55043#><#69633#> is the only natural number in [<#69634#><#55044#>0<#55044#><#69634#>, <#69635#><#55045#>1<#55045#><#69635#>);
  3. <#69636#><#55046#>(reset-aux<#55046#>\ <#55047#>call-status<#55047#>\ <#55048#>(vector-length<#55048#>\ <#55049#>call-status))<#55049#><#69636#> sets all fields of <#69637#><#55050#>call-status<#55050#><#69637#> to <#69638#><#55051#>false<#55051#><#69638#>.
The last example implies that we can define <#69639#><#55053#>reset<#55053#><#69639#> with <#69640#><#55054#>(reset-aux<#55054#>\ <#55055#>call-status<#55055#>\ <#55056#>(vector-length<#55056#>\ <#55057#>call-status))<#55057#><#69640#>. Equipped with examples, we can turn our attention to the definition. The key is to remember that the additional argument must be interpreted as an index into the vector. Keeping the example and the guideline in mind, let's look at each of the two cases separately:
  1. If <#69641#><#55059#>(zero?<#55059#>\ <#55060#>i)<#55060#><#69641#> holds, the function has no effect and produces <#69642#><#55061#>(<#55061#><#55062#>void<#55062#><#55063#>)<#55063#><#69642#>
  2. Otherwise <#69643#><#55064#>i<#55064#><#69643#> is positive. In that case, the natural recursion sets all fields in <#69644#><#55065#>call-status<#55065#><#69644#> with an index in [0,<#69645#><#55066#>(sub1<#55066#><#55067#> <#55067#>\ <#55068#>i)<#55068#><#69645#>) to <#69646#><#55069#>false<#55069#><#69646#>. Furthermore, to complete the task, the function must set the vector field with index <#69647#><#55070#>(sub1<#55070#>\ <#55071#>i)<#55071#><#69647#> to <#69648#><#55072#>false<#55072#><#69648#>. The combination of the two effects is achieved with a <#69649#><#55073#>begin<#55073#>-expression<#69649#> that sequences the natural recursion and the additional <#69650#><#55074#>vector-set!<#55074#><#69650#>.
Figure~#figreset#55076> puts everything together. The second clause in the definition of <#69651#><#55077#>reset-aux<#55077#><#69651#> first changes the vector at index <#69652#><#55078#>(sub1<#55078#>\ <#55079#>i)<#55079#><#69652#> and then uses the natural recursion. The result of the natural recursion is the result of the <#69653#><#55080#>begin<#55080#>-expression<#69653#>.
<#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#>

<#55164#>Exercise 41.2.14<#55164#> Use the examples to develop tests for <#69663#><#55166#>reset-aux<#55166#><#69663#>. Formulate the tests as boolean-valued expressions.~ external Solution<#69664#><#69664#> <#55172#>Exercise 41.2.15<#55172#> Develop the following variant of <#69665#><#55174#>reset<#55174#><#69665#>:
<#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#>.~ external Solution<#69673#><#69673#> <#55210#>Exercise 41.2.16<#55210#> Suppose we represent the position of an object with a vector and the velocity of an object with a second vector. Develop the function <#69674#><#55212#>move!<#55212#><#69674#>, which consumes a position vector and an equally long velocity vector. It modifies the position vector by adding in the numbers of the speed vector, field by field:
<#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.~ external Solution<#69678#><#69678#> <#55242#>Exercise 41.2.17<#55242#> Develop the function <#69679#><#55244#>vec-for-all<#55244#><#69679#>, which abstracts <#69680#><#55245#>reset-aux<#55245#><#69680#> and the auxiliary vector-processing function for <#69681#><#55246#>move!<#55246#><#69681#> from exercise~#expos0#55247>. It consumes two values: a function <#69682#><#55248#>f<#55248#><#69682#> and a vector <#69683#><#55249#>vec<#55249#><#69683#>. The function <#69684#><#55250#>f<#55250#><#69684#> consumes indices (<#69685#><#55251#>N<#55251#><#69685#>) and vector items. The result of <#69686#><#55252#>vec-for-all<#55252#><#69686#> is <#69687#><#55253#>(<#55253#><#55254#>void<#55254#><#55255#>)<#55255#><#69687#>; its effect is to apply <#69688#><#55256#>f<#55256#><#69688#> to each index and corresponding value in <#69689#><#55257#>vec<#55257#><#69689#>:
<#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#>.~ external Solution<#69699#><#69699#>
The last example covers the common situation when we wish to compute several numeric values at once and place them in a vector. In section~#secstatemanyexamples#55317> we saw that the use of effects is on occasion useful to communicate several results. In the same manner, it is sometimes best to create a vector and to modify it within the same function. Consider the problem of counting how many times each vowel occurs in a list of letters:
<#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#>
To fill the gaps in the template, we consider each of the two clauses separately:
  1. If <#69713#><#55498#>(empty?<#55498#>\ <#55499#>chars)<#55499#><#69713#> is <#69714#><#55500#>true<#55500#><#69714#>, the result is a vector of five <#69715#><#55501#>0<#55501#><#69715#>'s. After all, there are no vowels in an empty list.
  2. If <#69716#><#55502#>chars<#55502#><#69716#> isn't <#69717#><#55503#>empty<#55503#><#69717#>, the natural recursion counts how many vowels and which ones occur in <#69718#><#55504#>(rest<#55504#>\ <#55505#>chars)<#55505#><#69718#>. To get the correct result, we also have to check whether <#69719#><#55506#>(first<#55506#>\ <#55507#>chars)<#55507#><#69719#> is a vowel, and depending on the outcome, increase one of the vector fields. Since this kind of task is a separate, repeated task, we leave it to an auxiliary function:
    <#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.
Figure~#figcountvowels#55535> contains the complete definition of the main function. Defining the auxiliary function follows the recipe for non-recursive structure mutations.
<#55538#>Exercise 41.2.18<#55538#> Develop the function <#69727#><#55540#>count-a-vowel<#55540#><#69727#>. Then test the complete <#69728#><#55541#>count-vowels<#55541#><#69728#> program.~ external Solution<#69729#><#69729#> <#55547#>Exercise 41.2.19<#55547#> At the end of intermezzo~5, we could have defined <#69730#><#55549#>count-vowels<#55549#><#69730#> as shown in figure~#figanothervowel#55550>. This version does not use <#69731#><#55551#>vector-set!<#55551#><#69731#>, but constructs the vector directly using <#69732#><#55552#>build-vector<#55552#><#69732#>.
<#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#>
Measure the performance difference between <#69733#><#55650#>count-vowels-bv<#55650#><#69733#> and <#69734#><#55651#>count-vowels<#55651#><#69734#>. <#55652#>Hint:<#55652#> \ Define a function that generates a large list of random letters (with, say, 5,000 or 10,000 items). Explain the performance difference between <#69735#><#55653#>count-vowels-bv<#55653#><#69735#> and <#69736#><#55654#>count-vowels<#55654#><#69736#>. Does the explanation reflect the measured difference? What does this suggest concerning the <#69737#><#55655#>vector-set!<#55655#><#69737#> operation?~ external Solution<#69738#><#69738#> <#55661#>Exercise 41.2.20<#55661#> Develop <#69739#><#55663#>histogram<#55663#><#69739#>. The function consumes a list of grades between 0 and 100; it produces a vector of size 101 where each slot contains the number of grades at that level.~ external Solution<#69740#><#69740#> <#55669#>Exercise 41.2.21<#55669#> Develop <#69741#><#55671#>count-children<#55671#><#69741#>. The function consumes an descendant family tree, which is a family tree that leads from a family member to the descendants. It produces a vector with six fields. The first five slots contain the number of family members that have that many children; the sixth field contains the number of family members that have five or more children.~ external Solution<#69742#><#69742#>