Mutable Structures

Scheme structures are mutable. In <#52586#>Advanced Student<#52586#> Scheme, a structure definition such as
<#52591#>(define-structure<#52591#> <#52592#>posn<#52592#> <#52593#>(x<#52593#> <#52594#>y))<#52594#>
introduces six primitives, not just four:
  1. <#69290#><#52599#>make-posn<#52599#><#69290#>, the constructor;
  2. <#69291#><#52600#>posn-x<#52600#><#69291#> and <#69292#><#52601#>posn-y<#52601#><#69292#>, the selectors;
  3. <#69293#><#52602#>posn?<#52602#><#69293#>, the predicate; and
  4. <#69294#><#52603#>set-posn-x!<#52603#><#69294#> and <#69295#><#52604#>set-posn-y!<#52604#><#69295#>, the <#69296#><#52605#>MUTATORS<#52605#><#69296#>.
The mutators are operations that change the contents of a structure. Recall that we think of a structure as a box with compartments. For example, the structure
<#52611#>(make-posn<#52611#> <#52612#>3<#52612#> <#52613#>4)<#52613#>
should be visualized as a box with two compartments:

#picture52618#

A constructor creates a box; a selector extracts the value from a particular compartment; the predicate recognizes it; and the mutator changes the content of a compartment. That is, a mutator has an effect on its arguments; its result is the invisible value. Pictorially, we should imagine an evaluation step for an expression such as

<#52634#>(define<#52634#> <#52635#>p<#52635#> <#52636#>(make-posn<#52636#> <#52637#>3<#52637#> <#52638#>4))<#52638#>
<#52639#>(set-posn-x!<#52639#> <#52640#>p<#52640#> <#52641#>5)<#52641#> 
as a box with the old <#69306#><#52645#>x<#52645#><#69306#> value deleted and a new one inserted into the same box:

#picture52647#

Consider the following definitions:

<#52670#>(define-struct<#52670#> <#52671#>star<#52671#> <#52672#>(name<#52672#> <#52673#>instrument))<#52673#>
<#52674#>(define<#52674#> <#52675#>p<#52675#> <#52676#>(make-star<#52676#> <#52677#>'<#52677#><#52678#>PhilCollins<#52678#> <#52679#>'<#52679#><#52680#>drums))<#52680#> 
Let's consider the effect and computation of the following expression:
<#52688#>(b<#52688#><#52689#>egin<#52689#>
  <#52690#>(set-star-instrument!<#52690#> <#52691#>p<#52691#> <#52692#>'<#52692#><#52693#>vocals)<#52693#> 
  <#52694#>(list<#52694#> <#52695#>(star-instrument<#52695#> <#52696#>p)))<#52696#> 
According to our explanation, the first subexpression modifies the <#69321#><#52700#>instrument<#52700#><#69321#> field of the <#69322#><#52701#>star<#52701#><#69322#> structure named <#69323#><#52702#>p<#52702#><#69323#>; the second one produces a list of one item, the current value the <#69324#><#52703#>instrument<#52703#><#69324#> field in the structure name <#69325#><#52704#>p<#52704#><#69325#>. By analogy to section~#secimpstructfunc#52705>, the evaluation proceeds as follows:
  <#52710#>(define-struct<#52710#> <#52711#>star<#52711#> <#52712#>(name<#52712#> <#52713#>instrument))<#52713#>
  <#52714#>(define<#52714#> <#52715#>p<#52715#> <#52716#>(make-star<#52716#> <#52717#>'<#52717#><#52718#>PhilCollins<#52718#> <#52719#>'<#52719#><#52720#>drums))<#52720#> 
  <#52721#>(b<#52721#><#52722#>egin<#52722#> 
    <#52723#>(set-star-instrument!<#52723#> <#52724#>p<#52724#> <#52725#>'<#52725#><#52726#>vocals)<#52726#> 
    <#52727#>(list<#52727#> <#52728#>(star-instrument<#52728#> <#52729#>p)))<#52729#> 
<#52730#>=<#52730#> <#52731#>(define-struct<#52731#> <#52732#>star<#52732#> <#52733#>(name<#52733#> <#52734#>instrument))<#52734#> 
  <#52735#>(define<#52735#> <#52736#>p<#52736#> <#52737#>(make-star<#52737#> <#52738#>'<#52738#><#52739#>PhilCollins<#52739#> <#52740#>'<#52740#><#52741#>vocals))<#52741#> 
  <#52742#>(b<#52742#><#52743#>egin<#52743#> 
    <#52744#>(<#52744#><#52745#>void<#52745#><#52746#>)<#52746#> 
    <#52747#>(list<#52747#> <#52748#>(star-instrument<#52748#> <#52749#>p)))<#52749#> 
<#52750#>=<#52750#> <#52751#>(define-struct<#52751#> <#52752#>star<#52752#> <#52753#>(name<#52753#> <#52754#>instrument))<#52754#> 
  <#52755#>(define<#52755#> <#52756#>p<#52756#> <#52757#>(make-star<#52757#> <#52758#>'<#52758#><#52759#>PhilCollins<#52759#> <#52760#>'<#52760#><#52761#>vocals))<#52761#> 
  <#52762#>(list<#52762#> <#52763#>'<#52763#><#52764#>vocals)<#52764#> 
The first step changes one part of the value in the definition of <#69326#><#52768#>p<#52768#><#69326#>, but not the entire value. The second one extracts the current value of the <#69327#><#52769#>instrument<#52769#><#69327#> field and places it in a list. The introduction of mutators for structures requires two changes to our system of evaluation: rules:
  1. Every constructor expressions adds a definition with a new, unique name to the top level, unless it already occurs in a definition.
  2. A name that stands for a structure is a value.
We can understand these changes if we think of each structure as a function that manages services such as looking up the current value of a field and modifying the field. After all, <#69329#><#52774#>local<#52774#><#69329#> function definitions also create top-level definitions with unique names. And, the names of functions are values, too. Using these two new rules we can study the unusual behavior of mutators in more depth. Here is a first example:
<#52779#>(define-struct<#52779#> <#52780#>star<#52780#> <#52781#>(name<#52781#> <#52782#>instrument))<#52782#>
<#52783#>(define<#52783#> <#52784#>p<#52784#> <#52785#>(make-star<#52785#> <#52786#>'<#52786#><#52787#>PhilCollins<#52787#> <#52788#>'<#52788#><#52789#>drums))<#52789#> 
<#52790#>(define<#52790#> <#52791#>q<#52791#> <#52792#>p)<#52792#> 
<#52793#>(b<#52793#><#52794#>egin<#52794#> 
  <#52795#>(set-star-instrument!<#52795#> <#52796#>p<#52796#> <#52797#>'<#52797#><#52798#>vocals)<#52798#> 
  <#52799#>(list<#52799#> <#52800#>(star-instrument<#52800#> <#52801#>q)))<#52801#> 
It differs from the first in two ways. First, it defines <#69330#><#52805#>q<#52805#><#69330#> to be <#69331#><#52806#>p<#52806#><#69331#>. Second, the second subexpression of the <#69332#><#52807#>begin<#52807#>-expression<#69332#> refers to <#69333#><#52808#>q<#52808#><#69333#> not <#69334#><#52809#>p<#52809#><#69334#>. Let's check our understanding of the evaluation process:
  <#52814#>(define-struct<#52814#> <#52815#>star<#52815#> <#52816#>(name<#52816#> <#52817#>instrument))<#52817#>
  <#52818#>(define<#52818#> <#52819#>p<#52819#> <#52820#>(make-star<#52820#> <#52821#>'<#52821#><#52822#>PhilCollins<#52822#> <#52823#>'<#52823#><#52824#>drums))<#52824#> 
  <#52825#>(define<#52825#> <#52826#>q<#52826#> <#52827#>p)<#52827#> 
  <#52828#>(b<#52828#><#52829#>egin<#52829#> 
    <#52830#>(set-star-instrument!<#52830#> <#52831#>p<#52831#> <#52832#>'<#52832#><#52833#>vocals)<#52833#> 
    <#52834#>(list<#52834#> <#52835#>(star-instrument<#52835#> <#52836#>q)))<#52836#> 
<#52837#>=<#52837#> <#52838#>(define-struct<#52838#> <#52839#>star<#52839#> <#52840#>(name<#52840#> <#52841#>instrument))<#52841#> 
  <#52842#>(define<#52842#> <#52843#>p<#52843#> <#52844#>(make-star<#52844#> <#52845#>'<#52845#><#52846#>PhilCollins<#52846#> <#52847#>'<#52847#><#52848#>vocals))<#52848#> 
  <#52849#>(define<#52849#> <#52850#>q<#52850#> <#52851#>p)<#52851#> 
  <#52852#>(b<#52852#><#52853#>egin<#52853#> 
    <#52854#>(<#52854#><#52855#>void<#52855#><#52856#>)<#52856#> 
    <#52857#>(list<#52857#> <#52858#>(star-instrument<#52858#> <#52859#>q)))<#52859#> 
As before, the first step changes one part of the definition of <#69335#><#52863#>p<#52863#><#69335#>. The second step is to look up <#69336#><#52864#>q<#52864#><#69336#>'s current value:
   <#52869#>...<#52869#>
<#52870#>=<#52870#> <#52871#>(define-struct<#52871#> <#52872#>star<#52872#> <#52873#>(name<#52873#> <#52874#>instrument))<#52874#> 
  <#52875#>(define<#52875#> <#52876#>p<#52876#> <#52877#>(make-star<#52877#> <#52878#>'<#52878#><#52879#>PhilCollins<#52879#> <#52880#>'<#52880#><#52881#>vocals))<#52881#> 
  <#52882#>(define<#52882#> <#52883#>q<#52883#> <#52884#>p)<#52884#> 
  <#52885#>(list<#52885#> <#52886#>(star-instrument<#52886#> <#52887#>p))<#52887#> 
<#52888#>=<#52888#> <#52889#>(define-struct<#52889#> <#52890#>star<#52890#> <#52891#>(name<#52891#> <#52892#>instrument))<#52892#> 
  <#52893#>(define<#52893#> <#52894#>p<#52894#> <#52895#>(make-star<#52895#> <#52896#>'<#52896#><#52897#>PhilCollins<#52897#> <#52898#>'<#52898#><#52899#>vocals))<#52899#> 
  <#52900#>(define<#52900#> <#52901#>q<#52901#> <#52902#>p)<#52902#> 
  <#52903#>(list<#52903#> <#52904#>'<#52904#><#52905#>vocals)<#52905#> 
Because <#69337#><#52909#>q<#52909#><#69337#> is <#69338#><#52910#>p<#52910#><#69338#> and the current value of the <#69339#><#52911#>instrument<#52911#><#69339#> field of <#69340#><#52912#>p<#52912#><#69340#> instrument is <#69341#><#52913#>'<#52913#><#52914#>vocals<#52914#><#69341#>, the result is again <#69342#><#52915#>(list<#52915#>\ <#52916#>'<#52916#><#52917#>vocals)<#52917#><#69342#>. What we have just seen is the effect of <#69343#><#52918#>SHARING<#52918#><#69343#> (the effects of mutators), which means that a modification of a struture affects the program in more than one place. Sharing is also visible inside of lists as our second example shows:
<#52923#>(define-struct<#52923#> <#52924#>star<#52924#> <#52925#>(name<#52925#> <#52926#>instrument))<#52926#>
<#52927#>(define<#52927#> <#52928#>q<#52928#> <#52929#>(list<#52929#> <#52930#>(make-star<#52930#> <#52931#>'<#52931#><#52932#>PhilCollins<#52932#> <#52933#>'<#52933#><#52934#>drums)))<#52934#> 
<#52935#>(b<#52935#><#52936#>egin<#52936#> 
  <#52937#>(set-star-instrument!<#52937#> <#52938#>(first<#52938#> <#52939#>q)<#52939#> <#52940#>'<#52940#><#52941#>vocals)<#52941#> 
  <#52942#>(list<#52942#> <#52943#>(star-instrument<#52943#> <#52944#>(first<#52944#> <#52945#>q))))<#52945#> 
Here, the right-hand side of the definition of <#69344#><#52949#>q<#52949#><#69344#> is an expression whose only subexpression isn't a value. More precisely, it is a structure expression that must be evaluated:
  <#52954#>...<#52954#> 
<#52955#>=<#52955#> <#52956#>(define-struct<#52956#> <#52957#>star<#52957#> <#52958#>(name<#52958#> <#52959#>instrument))<#52959#> 
  <#52960#>(define<#52960#> <#52961#>p<#52961#> <#52962#>(make-star<#52962#> <#52963#>'<#52963#><#52964#>PhilCollins<#52964#> <#52965#>'<#52965#><#52966#>drums))<#52966#> 
  <#52967#>(define<#52967#> <#52968#>q<#52968#> <#52969#>(list<#52969#> <#52970#>p))<#52970#> 
  <#52971#>(b<#52971#><#52972#>egin<#52972#> 
    <#52973#>(set-star-instrument!<#52973#> <#52974#>(first<#52974#> <#52975#>q)<#52975#> <#52976#>'<#52976#><#52977#>vocals)<#52977#> 
    <#52978#>(list<#52978#> <#52979#>(star-instrument<#52979#> <#52980#>(first<#52980#> <#52981#>q))))<#52981#> 
<#52982#>=<#52982#> <#52983#>(define-struct<#52983#> <#52984#>star<#52984#> <#52985#>(name<#52985#> <#52986#>instrument))<#52986#> 
  <#52987#>(define<#52987#> <#52988#>p<#52988#> <#52989#>(make-star<#52989#> <#52990#>'<#52990#><#52991#>PhilCollins<#52991#> <#52992#>'<#52992#><#52993#>drums))<#52993#> 
  <#52994#>(define<#52994#> <#52995#>q<#52995#> <#52996#>(list<#52996#> <#52997#>p))<#52997#> 
  <#52998#>(b<#52998#><#52999#>egin<#52999#> 
    <#53000#>(set-star-instrument!<#53000#> <#53001#>p<#53001#> <#53002#>'<#53002#><#53003#>vocals)<#53003#> 
    <#53004#>(list<#53004#> <#53005#>(star-instrument<#53005#> <#53006#>(first<#53006#> <#53007#>q))))<#53007#> 
Thus the first step is to introduce a new definition, for which we chose <#69345#><#53011#>p<#53011#><#69345#> as the name. The second step replaces <#69346#><#53012#>(first<#53012#>\ <#53013#>q)<#53013#><#69346#> by <#69347#><#53014#>p<#53014#><#69347#>, because <#69348#><#53015#>q<#53015#><#69348#> is a list of one item: <#69349#><#53016#>p<#53016#><#69349#>. The rest proceeds almost as above:
<#53021#>...<#53021#> 
<#53022#>=<#53022#> <#53023#>(define-struct<#53023#> <#53024#>star<#53024#> <#53025#>(name<#53025#> <#53026#>instrument))<#53026#> 
  <#53027#>(define<#53027#> <#53028#>p<#53028#> <#53029#>(make-star<#53029#> <#53030#>'<#53030#><#53031#>PhilCollins<#53031#> <#53032#>'<#53032#><#53033#>vocals))<#53033#> 
  <#53034#>(define<#53034#> <#53035#>q<#53035#> <#53036#>(list<#53036#> <#53037#>p))<#53037#> 
  <#53038#>(b<#53038#><#53039#>egin<#53039#> 
    <#53040#>(<#53040#><#53041#>void<#53041#><#53042#>)<#53042#> 
    <#53043#>(list<#53043#> <#53044#>(star-instrument<#53044#> <#53045#>(first<#53045#> <#53046#>q))))<#53046#> 
<#53047#>=<#53047#> <#53048#>(define-struct<#53048#> <#53049#>star<#53049#> <#53050#>(name<#53050#> <#53051#>instrument))<#53051#> 
  <#53052#>(define<#53052#> <#53053#>p<#53053#> <#53054#>(make-star<#53054#> <#53055#>'<#53055#><#53056#>PhilCollins<#53056#> <#53057#>'<#53057#><#53058#>vocals))<#53058#> 
  <#53059#>(define<#53059#> <#53060#>q<#53060#> <#53061#>(list<#53061#> <#53062#>p))<#53062#> 
  <#53063#>(list<#53063#> <#53064#>(star-instrument<#53064#> <#53065#>p))<#53065#> 
<#53066#>=<#53066#> <#53067#>(define-struct<#53067#> <#53068#>star<#53068#> <#53069#>(name<#53069#> <#53070#>instrument))<#53070#> 
  <#53071#>(define<#53071#> <#53072#>p<#53072#> <#53073#>(make-star<#53073#> <#53074#>'<#53074#><#53075#>PhilCollins<#53075#> <#53076#>'<#53076#><#53077#>vocals))<#53077#> 
  <#53078#>(define<#53078#> <#53079#>q<#53079#> <#53080#>(list<#53080#> <#53081#>p))<#53081#> 
  <#53082#>(list<#53082#> <#53083#>'<#53083#><#53084#>vocals)<#53084#> 
Finally, effects can be shared among items in different lists. Take a look at this third variant of our program:
<#53092#>(define-struct<#53092#> <#53093#>star<#53093#> <#53094#>(name<#53094#> <#53095#>instrument))<#53095#>
<#53096#>(define<#53096#> <#53097#>q<#53097#> <#53098#>(list<#53098#> <#53099#>(make-star<#53099#> <#53100#>'<#53100#><#53101#>PhilCollins<#53101#> <#53102#>'<#53102#><#53103#>drums)))<#53103#> 
<#53104#>(define<#53104#> <#53105#>r<#53105#> <#53106#>(list<#53106#> <#53107#>(first<#53107#> <#53108#>q)<#53108#> <#53109#>(star-instrument<#53109#> <#53110#>(first<#53110#> <#53111#>q))))<#53111#> 
<#53112#>(b<#53112#><#53113#>egin<#53113#> 
  <#53114#>(set-star-instrument!<#53114#> <#53115#>(first<#53115#> <#53116#>q)<#53116#> <#53117#>'<#53117#><#53118#>vocals)<#53118#> 
  <#53119#>(list<#53119#> <#53120#>(star-instrument<#53120#> <#53121#>(first<#53121#> <#53122#>r))))<#53122#> 
The new definition introduces the variable <#69350#><#53126#>r<#53126#><#69350#>, which stands for a list that contains two items. Let's use our new rules to determine the values and the effects of this program:
<#53131#>...<#53131#> 
<#53132#>=<#53132#> <#53133#>(define-struct<#53133#> <#53134#>star<#53134#> <#53135#>(name<#53135#> <#53136#>instrument))<#53136#> 
  <#53137#>(define<#53137#> <#53138#>p<#53138#> <#53139#>(make-star<#53139#> <#53140#>'<#53140#><#53141#>PhilCollins<#53141#> <#53142#>'<#53142#><#53143#>drums))<#53143#> 
  <#53144#>(define<#53144#> <#53145#>q<#53145#> <#53146#>(list<#53146#> <#53147#>p))<#53147#> 
  <#53148#>(define<#53148#> <#53149#>r<#53149#> <#53150#>(list<#53150#> <#53151#>(first<#53151#> <#53152#>q)<#53152#> <#53153#>(star-instrument<#53153#> <#53154#>(first<#53154#> <#53155#>q))))<#53155#> 
  <#53156#>(b<#53156#><#53157#>egin<#53157#> 
    <#53158#>(set-star-instrument!<#53158#> <#53159#>(first<#53159#> <#53160#>q)<#53160#> <#53161#>'<#53161#><#53162#>vocals)<#53162#> 
    <#53163#>(list<#53163#> <#53164#>(star-instrument<#53164#> <#53165#>(first<#53165#> <#53166#>r))))<#53166#> 
<#53167#>=<#53167#> <#53168#>(define-struct<#53168#> <#53169#>star<#53169#> <#53170#>(name<#53170#> <#53171#>instrument))<#53171#> 
  <#53172#>(define<#53172#> <#53173#>p<#53173#> <#53174#>(make-star<#53174#> <#53175#>'<#53175#><#53176#>PhilCollins<#53176#> <#53177#>'<#53177#><#53178#>drums))<#53178#> 
  <#53179#>(define<#53179#> <#53180#>q<#53180#> <#53181#>(list<#53181#> <#53182#>p))<#53182#> 
  <#53183#>(define<#53183#> <#53184#>r<#53184#> <#53185#>(list<#53185#> <#53186#>p<#53186#> <#53187#>(star-instrument<#53187#> <#53188#>p)))<#53188#> 
  <#53189#>(b<#53189#><#53190#>egin<#53190#> 
    <#53191#>(set-star-instrument!<#53191#> <#53192#>(first<#53192#> <#53193#>q)<#53193#> <#53194#>'<#53194#><#53195#>vocals)<#53195#> 
    <#53196#>(list<#53196#> <#53197#>(star-instrument<#53197#> <#53198#>(first<#53198#> <#53199#>r))))<#53199#> 
<#53200#>=<#53200#> <#53201#>(define-struct<#53201#> <#53202#>star<#53202#> <#53203#>(name<#53203#> <#53204#>instrument))<#53204#> 
  <#53205#>(define<#53205#> <#53206#>p<#53206#> <#53207#>(make-star<#53207#> <#53208#>'<#53208#><#53209#>PhilCollins<#53209#> <#53210#>'<#53210#><#53211#>drums))<#53211#> 
  <#53212#>(define<#53212#> <#53213#>q<#53213#> <#53214#>(list<#53214#> <#53215#>p))<#53215#> 
  <#53216#>(define<#53216#> <#53217#>r<#53217#> <#53218#>(list<#53218#> <#53219#>p<#53219#> <#53220#>'<#53220#><#53221#>drums))<#53221#> 
  <#53222#>(b<#53222#><#53223#>egin<#53223#> 
    <#53224#>(set-star-instrument!<#53224#> <#53225#>(first<#53225#> <#53226#>q)<#53226#> <#53227#>'<#53227#><#53228#>vocals)<#53228#> 
    <#53229#>(list<#53229#> <#53230#>(star-instrument<#53230#> <#53231#>(first<#53231#> <#53232#>r))))<#53232#> 
As above, the first step introduces a definition for the new <#69351#><#53236#>star<#53236#><#69351#> structure. The second and third step create the list named <#69352#><#53237#>r<#53237#><#69352#>, which contains <#69353#><#53238#>p<#53238#><#69353#>, the newly created structure, and <#69354#><#53239#>'<#53239#><#53240#>vocals<#53240#><#69354#>, its current <#69355#><#53241#>instrument<#53241#><#69355#> value. The next step selects the first item from <#69356#><#53242#>q<#53242#><#69356#> and modifies its <#69357#><#53243#>instrument<#53243#><#69357#> field:
<#53248#>...<#53248#> 
<#53249#>=<#53249#> <#53250#>(define-struct<#53250#> <#53251#>star<#53251#> <#53252#>(name<#53252#> <#53253#>instrument))<#53253#> 
  <#53254#>(define<#53254#> <#53255#>p<#53255#> <#53256#>(make-star<#53256#> <#53257#>'<#53257#><#53258#>PhilCollins<#53258#> <#53259#>'<#53259#><#53260#>vocals))<#53260#> 
  <#53261#>(define<#53261#> <#53262#>q<#53262#> <#53263#>(list<#53263#> <#53264#>p))<#53264#> 
  <#53265#>(define<#53265#> <#53266#>r<#53266#> <#53267#>(list<#53267#> <#53268#>p<#53268#> <#53269#>'<#53269#><#53270#>drums))<#53270#> 
  <#53271#>(b<#53271#><#53272#>egin<#53272#> 
    <#53273#>(<#53273#><#53274#>void<#53274#><#53275#>)<#53275#> 
    <#53276#>(list<#53276#> <#53277#>(star-instrument<#53277#> <#53278#>(first<#53278#> <#53279#>r))))<#53279#> 
<#53280#>=<#53280#> <#53281#>(define-struct<#53281#> <#53282#>star<#53282#> <#53283#>(name<#53283#> <#53284#>instrument))<#53284#> 
  <#53285#>(define<#53285#> <#53286#>p<#53286#> <#53287#>(make-star<#53287#> <#53288#>'<#53288#><#53289#>PhilCollins<#53289#> <#53290#>'<#53290#><#53291#>vocals))<#53291#> 
  <#53292#>(define<#53292#> <#53293#>q<#53293#> <#53294#>(list<#53294#> <#53295#>p))<#53295#> 
  <#53296#>(define<#53296#> <#53297#>r<#53297#> <#53298#>(list<#53298#> <#53299#>p<#53299#> <#53300#>'<#53300#><#53301#>drums))<#53301#> 
  <#53302#>(list<#53302#> <#53303#>(star-instrument<#53303#> <#53304#>p))<#53304#> 
<#53305#>=<#53305#> <#53306#>(define-struct<#53306#> <#53307#>star<#53307#> <#53308#>(name<#53308#> <#53309#>instrument))<#53309#> 
  <#53310#>(define<#53310#> <#53311#>p<#53311#> <#53312#>(make-star<#53312#> <#53313#>'<#53313#><#53314#>PhilCollins<#53314#> <#53315#>'<#53315#><#53316#>vocals))<#53316#> 
  <#53317#>(define<#53317#> <#53318#>q<#53318#> <#53319#>(list<#53319#> <#53320#>p))<#53320#> 
  <#53321#>(define<#53321#> <#53322#>r<#53322#> <#53323#>(list<#53323#> <#53324#>p<#53324#> <#53325#>'<#53325#><#53326#>drums))<#53326#> 
  <#53327#>(list<#53327#> <#53328#>'<#53328#><#53329#>vocals)<#53329#> 
Because <#69358#><#53333#>r<#53333#><#69358#> contains <#69359#><#53334#>p<#53334#><#69359#> as the first item and because the instrument field of <#69360#><#53335#>p<#53335#><#69360#> is still <#69361#><#53336#>'<#53336#><#53337#>vocals<#53337#><#69361#>, the result is <#69362#><#53338#>(list<#53338#>\ <#53339#>'<#53339#><#53340#>vocals)<#53340#><#69362#> here, too. But, this program still has some knowledge of <#69363#><#53341#>'<#53341#><#53342#>drums<#53342#><#69363#>, the original value of the <#69364#><#53343#>star<#53343#><#69364#> structure. In summary, mutators give us more power than constructors and selectors. Instead of just creating new structures and revealing their contents, we can now change their contents, while the structures remain the same. Next we must contemplate what this means for the design of programs.
<#53346#>Exercise 40.3.1<#53346#> Name the mutators that the following structure definitions introduce:
  1. <#69365#><#53349#>(define-struct<#53349#>\ <#53350#>movie<#53350#>\ <#53351#>(title<#53351#>\ <#53352#>producer))<#53352#><#69365#>
  2. <#69366#><#53353#>(define-struct<#53353#>\ <#53354#>boyfriend<#53354#>\ <#53355#>(name<#53355#>\ <#53356#>hair<#53356#>\ <#53357#>eyes<#53357#>\ <#53358#>phone))<#53358#><#69366#>
  3. <#69367#><#53359#>(define-struct<#53359#>\ <#53360#>cheerleader<#53360#>\ <#53361#>(name<#53361#>\ <#53362#>number))<#53362#><#69367#>
  4. <#69368#><#53363#>(define-struct<#53363#>\ <#53364#>CD<#53364#>\ <#53365#>(artist<#53365#>\ <#53366#>title<#53366#>\ <#53367#>price))<#53367#><#69368#>
  5. <#69369#><#53368#>(define-struct<#53368#>\ <#53369#>sweater<#53369#>\ <#53370#>(material<#53370#>\ <#53371#>size<#53371#>\ <#53372#>producer))<#53372#><#69369#>~ external Solution<#69370#><#69370#>
<#53379#>Exercise 40.3.2<#53379#> Develop the function <#69371#><#53381#>swap-posn<#53381#><#69371#>, which consumes a <#69372#><#53382#>posn<#53382#><#69372#> structure and swaps the values in the two fields. Its result is <#69373#><#53383#>(<#53383#><#53384#>void<#53384#><#53385#>)<#53385#><#69373#>.~ external Solution<#69374#><#69374#> <#53391#>Exercise 40.3.3<#53391#> Develop the function <#69375#><#53393#>one-more-date<#53393#><#69375#>, which consumes a <#69376#><#53394#>girlfriends<#53394#><#69376#> structure and increases the contents of the <#69377#><#53395#>number-past-dates<#53395#><#69377#> field by <#69378#><#53396#>1<#53396#><#69378#>. The structure definition is
<#53401#>(define-struct<#53401#> <#53402#>girlfriends<#53402#> <#53403#>(name<#53403#> <#53404#>hair<#53404#> <#53405#>eyes<#53405#> <#53406#>number-past-dates))<#53406#>
The result of <#69379#><#53410#>one-more-date<#53410#><#69379#> is <#69380#><#53411#>(<#53411#><#53412#>void<#53412#><#53413#>)<#53413#><#69380#>.~ external Solution<#69381#><#69381#> <#53419#>Exercise 40.3.4<#53419#> Evaluate the following program, step by step:
<#53425#>(define-struct<#53425#> <#53426#>cheerleader<#53426#> <#53427#>(name<#53427#> <#53428#>number))<#53428#>
<#53429#>(define<#53429#> <#53430#>A<#53430#> <#53431#>(make-cheerleader<#53431#> <#53432#>'<#53432#><#53433#>JoAnn<#53433#> <#53434#>2))<#53434#> 
<#53435#>(define<#53435#> <#53436#>B<#53436#> <#53437#>(make-cheerleader<#53437#> <#53438#>'<#53438#><#53439#>Belle<#53439#> <#53440#>1))<#53440#> 
<#53441#>(define<#53441#> <#53442#>C<#53442#> <#53443#>(make-cheerleader<#53443#> <#53444#>'<#53444#><#53445#>Krissy<#53445#> <#53446#>1))<#53446#> 
<#53447#>(define<#53447#> <#53448#>all<#53448#> <#53449#>(list<#53449#> <#53450#>A<#53450#> <#53451#>B<#53451#> <#53452#>C))<#53452#> 
<#53453#>(l<#53453#><#53454#>ist<#53454#> 
  <#53455#>(cheerleader-number<#53455#> <#53456#>(second<#53456#> <#53457#>all))<#53457#> 
  <#53458#>(b<#53458#><#53459#>egin<#53459#> 
    <#53460#>(set-cheerleader-number!<#53460#> <#53461#>(second<#53461#> <#53462#>all)<#53462#> <#53463#>17)<#53463#> 
    <#53464#>(cheerleader-number<#53464#> <#53465#>(second<#53465#> <#53466#>all))))<#53466#> 
Underline in the last program where definitions differ from the initial program. ~ external Solution<#69382#><#69382#> <#53475#>Exercise 40.3.5<#53475#> Evaluate the following program:
<#53481#>(define-struct<#53481#> <#53482#>CD<#53482#> <#53483#>(artist<#53483#> <#53484#>title<#53484#> <#53485#>price))<#53485#>
<#53486#>(d<#53486#><#53487#>efine<#53487#> <#53488#>in-stock<#53488#> 
  <#53489#>(l<#53489#><#53490#>ist<#53490#> 
    <#53491#>(<#53491#><#53492#>(make-CD<#53492#> <#53493#>'<#53493#><#53494#>R.E.M<#53494#> <#53495#>``New<#53495#> <#53496#>Adventures<#53496#> <#53497#>in<#53497#> <#53498#>Hi-fi''<#53498#> <#53499#>0)<#53499#> 
     <#53500#>(make-CD<#53500#> <#53501#>'<#53501#><#53502#>France<#53502#> <#53503#>``simple<#53503#> <#53504#>je''<#53504#> <#53505#>0)<#53505#> 
     <#53506#>(make-CD<#53506#> <#53507#>'<#53507#><#53508#>Cranberries<#53508#> <#53509#>``no<#53509#> <#53510#>need<#53510#> <#53511#>to<#53511#> <#53512#>argue''<#53512#> <#53513#>0))))<#53513#> 
<#53514#>(b<#53514#><#53515#>egin<#53515#> 
  <#53516#>(set-CD-price!<#53516#> <#53517#>(first<#53517#> <#53518#>in-stock)<#53518#> <#53519#>12)<#53519#> 
  <#53520#>(set-CD-price!<#53520#> <#53521#>(second<#53521#> <#53522#>in-stock)<#53522#> <#53523#>19)<#53523#> 
  <#53524#>(set-CD-price!<#53524#> <#53525#>(third<#53525#> <#53526#>in-stock)<#53526#> <#53527#>11)<#53527#> 
  <#53528#>(+<#53528#> <#53529#>(CD-price<#53529#> <#53530#>(first<#53530#> <#53531#>in-stock))<#53531#> 
     <#53532#>(CD-price<#53532#> <#53533#>(second<#53533#> <#53534#>in-stock))<#53534#> 
     <#53535#>(CD-price<#53535#> <#53536#>(third<#53536#> <#53537#>in-stock))))<#53537#> 
Show every step.~ external Solution<#69383#><#69383#>