Functions that Change Memory

Once we have the state variables and their initializers in place, we turn our attention to the design of functions that modify a program's memory. Unlike the functions in the preceding parts of the book, the memory-changing functions not only consume and produce data, they also affect the definitions of the state variables. We therefore speak of the <#68236#><#45386#>EFFECT<#45386#><#68236#> that functions have on the state variables.
<#71717#>;; <#45391#>Data Def.<#45391#>: A <#68237#><#45392#>TL-color<#45392#><#68237#> is either <#68238#><#45393#>'<#45393#><#45394#>green<#45394#><#68238#>, <#68239#><#45395#>'<#45395#><#45396#>yellow<#45396#><#68239#>, or <#68240#><#45397#>'<#45397#><#45398#>red<#45398#><#68240#>.<#71717#>
<#68241#>;; <#45399#>State Variable<#45399#>: <#68241#> 
<#71718#>;; <#68242#><#45400#>current-color<#45400#> <#45401#>:<#45401#> <#45402#>TL-color<#45402#><#68242#><#71718#> 
<#45403#>;; to keep track of the current color of the traffic light<#45403#> 
<#45404#>(define<#45404#> <#45405#>current-color<#45405#> <#45406#>'<#45406#><#45407#>red)<#45407#> 
<#71719#>;; <#45408#>Contract<#45408#>: <#68243#><#45409#>next<#45409#> <#45410#>:<#45410#> <#45411#><#45411#><#45412#>-;SPMgt;<#45412#><#45413#><#45413#> <#45414#>void<#45414#><#68243#><#71719#> 
<#71720#>;; <#45415#>Purpose<#45415#>: the function always produces <#68244#><#45416#>(<#45416#><#45417#>void<#45417#><#45418#>)<#45418#><#68244#><#71720#> 
<#71721#>;; <#45419#>Effect<#45419#>: to change <#68245#><#45420#>current-color<#45420#><#68245#> from <#68246#><#45421#>'<#45421#><#45422#>green<#45422#><#68246#> to <#68247#><#45423#>'<#45423#><#45424#>yellow<#45424#><#68247#>, <#71721#> 
<#71722#>;; <#68248#><#45425#>'<#45425#><#45426#>yellow<#45426#><#68248#> to <#68249#><#45427#>'<#45427#><#45428#>red<#45428#><#68249#>, and <#68250#><#45429#>'<#45429#><#45430#>red<#45430#><#68250#> to <#68251#><#45431#>'<#45431#><#45432#>green<#45432#><#68251#><#71722#> 
<#68252#>;; <#45433#>Header<#45433#>: omitted for this particular example<#68252#> 
<#68253#>;; <#45434#>Examples<#45434#>: <#68253#> 
<#71723#>;; if <#68254#><#45435#>current-color<#45435#><#68254#> is <#68255#><#45436#>'<#45436#><#45437#>green<#45437#><#68255#> and we evaluate <#68256#><#45438#>(next)<#45438#><#68256#>, then <#68257#><#45439#>current-color<#45439#><#68257#> is <#68258#><#45440#>'<#45440#><#45441#>yellow<#45441#><#68258#><#71723#> 
<#71724#>;; if <#68259#><#45442#>current-color<#45442#><#68259#> is <#68260#><#45443#>'<#45443#><#45444#>yellow<#45444#><#68260#> and we evaluate <#68261#><#45445#>(next)<#45445#><#68261#>, then <#68262#><#45446#>current-color<#45446#><#68262#> is <#68263#><#45447#>'<#45447#><#45448#>red<#45448#><#68263#><#71724#> 
<#71725#>;; if <#68264#><#45449#>current-color<#45449#><#68264#> is <#68265#><#45450#>'<#45450#><#45451#>red<#45451#><#68265#> and we evaluate <#68266#><#45452#>(next)<#45452#><#68266#>, then <#68267#><#45453#>current-color<#45453#><#68267#> is <#68268#><#45454#>'<#45454#><#45455#>green<#45455#><#68268#><#71725#> 
<#68269#>;; <#45456#>Template<#45456#>: data-directed on state-variable that is to be mutated<#68269#> 
<#71726#>;; <#68270#><#45457#>(define<#45457#> <#45458#>(f)<#45458#><#68270#><#71726#> 
<#71727#>;; ~~<#68271#> <#45459#>(cond<#45459#><#68271#><#71727#> 
<#71728#>;; <#68272#> <#45460#>[<#45460#><#45461#>(symbol=?<#45461#> <#45462#>'<#45462#><#45463#>green<#45463#> <#45464#>current-color)<#45464#> <#45465#>(set!<#45465#> <#45466#>current-color<#45466#> <#45467#>...)]<#45467#><#68272#><#71728#> 
<#71729#>;; <#68273#> <#45468#>[<#45468#><#45469#>(symbol=?<#45469#> <#45470#>'<#45470#><#45471#>yellow<#45471#> <#45472#>current-color)<#45472#> <#45473#>(set!<#45473#> <#45474#>current-color<#45474#> <#45475#>...)]<#45475#><#68273#><#71729#> 
<#71730#>;; <#68274#> <#45476#>[<#45476#><#45477#>(symbol=?<#45477#> <#45478#>'<#45478#><#45479#>red<#45479#> <#45480#>current-color)<#45480#> <#45481#>(set!<#45481#> <#45482#>current-color<#45482#> <#45483#>...)]<#45483#><#45484#>))<#45484#><#68274#><#71730#> 
<#68275#>;; <#45485#>Definition<#45485#>:<#68275#> 
<#45486#>(d<#45486#><#45487#>efine<#45487#> <#45488#>(next)<#45488#> 
  <#45489#>(c<#45489#><#45490#>ond<#45490#> 
    <#45491#>[<#45491#><#45492#>(symbol=?<#45492#> <#45493#>'<#45493#><#45494#>green<#45494#> <#45495#>current-color)<#45495#> <#45496#>(set!<#45496#> <#45497#>current-color<#45497#> <#45498#>'<#45498#><#45499#>yellow)]<#45499#> 
    <#45500#>[<#45500#><#45501#>(symbol=?<#45501#> <#45502#>'<#45502#><#45503#>yellow<#45503#> <#45504#>current-color)<#45504#> <#45505#>(set!<#45505#> <#45506#>current-color<#45506#> <#45507#>'<#45507#><#45508#>red)]<#45508#> 
    <#45509#>[<#45509#><#45510#>(symbol=?<#45510#> <#45511#>'<#45511#><#45512#>red<#45512#> <#45513#>current-color)<#45513#> <#45514#>(set!<#45514#> <#45515#>current-color<#45515#> <#45516#>'<#45516#><#45517#>green)]<#45517#><#45518#>))<#45518#> 
  
<#68276#>;; <#45519#>Tests<#45519#>:<#68276#> 
<#45520#>(begin<#45520#> <#45521#>(set!<#45521#> <#45522#>current-color<#45522#> <#45523#>'<#45523#><#45524#>green)<#45524#> <#45525#>(next)<#45525#> <#45526#>(symbol=?<#45526#> <#45527#>current-color<#45527#> <#45528#>'<#45528#><#45529#>yellow))<#45529#> 
<#45530#>(begin<#45530#> <#45531#>(set!<#45531#> <#45532#>current-color<#45532#> <#45533#>'<#45533#><#45534#>yellow)<#45534#> <#45535#>(next)<#45535#> <#45536#>(symbol=?<#45536#> <#45537#>current-color<#45537#> <#45538#>'<#45538#><#45539#>red))<#45539#> 
<#45540#>(begin<#45540#> <#45541#>(set!<#45541#> <#45542#>current-color<#45542#> <#45543#>'<#45543#><#45544#>red)<#45544#> <#45545#>(next)<#45545#> <#45546#>(symbol=?<#45546#> <#45547#>current-color<#45547#> <#45548#>'<#45548#><#45549#>green))<#45549#> 
<#45553#>Figure: The design recipe for state variables: a complete example<#45553#>
Let us now take a look at the stages of our most basic design recipe and how we can accommodate effects on state variables:
Data Analysis:
Even functions that affect the state of variables consume and (possibly) produce data. Thus we still need to analyze how to represent information and, if necessary, introduce structure and data definitions. For example, the traffic-light example benefits from the data definition for <#68277#><#45556#>TL-color<#45556#><#68277#> (see above).
Contract, Purpose, and Effect:
The first major change concerns the second step. In addition to specifying what a function consumes and produces, we must also write down which variables it affects and how it affects those state variables. The effect of a function on state variables must be consistent with the purpose statement of a variable. Consider the traffic-light example again. It requires a function that switches the color of the traffic light in accordance with the traffic laws. The function checks the variable <#68278#><#45557#>current-color<#45557#><#68278#> and affects its state. Here is how we should specify this function:
<#71731#>;; <#68279#><#45562#>next<#45562#> <#45563#>:<#45563#> <#45564#><#45564#><#45565#>-;SPMgt;<#45565#><#45566#><#45566#> <#45567#>void<#45567#><#68279#><#71731#>
<#71732#>;; effect: to change <#68280#><#45568#>current-color<#45568#><#68280#> from <#68281#><#45569#>'<#45569#><#45570#>green<#45570#><#68281#> to <#68282#><#45571#>'<#45571#><#45572#>yellow<#45572#><#68282#>, <#71732#> 
<#71733#>;; <#68283#><#45573#>'<#45573#><#45574#>yellow<#45574#><#68283#> to <#68284#><#45575#>'<#45575#><#45576#>red<#45576#><#68284#>, and <#68285#><#45577#>'<#45577#><#45578#>red<#45578#><#68285#> to <#68286#><#45579#>'<#45579#><#45580#>green<#45580#><#68286#><#71733#> 
<#45581#>(define<#45581#> <#45582#>(next)<#45582#> <#45583#>...)<#45583#> 
The function consumes no data and always produces the invisible value; in Scheme this value is called <#68287#><#45587#>void<#45587#><#68287#>. Because the function has no purpose in the traditional sense, it is accompanied by an effect statement only. Here is the specification for <#68288#><#45588#>add-to-address-book<#45588#><#68288#>:
<#71734#>;; <#68289#><#45593#>add-to-address-book<#45593#> <#45594#>:<#45594#> <#45595#>symbol<#45595#> <#45596#>number<#45596#> <#45597#><#45597#><#45598#>-;SPMgt;<#45598#><#45599#><#45599#> <#45600#>void<#45600#><#68289#><#71734#>
<#71735#>;; effect: to add <#68290#><#45601#>(list<#45601#> <#45602#>name<#45602#> <#45603#>phone)<#45603#><#68290#> to the front of <#68291#><#45604#>address-book<#45604#><#68291#><#71735#> 
<#45605#>(define<#45605#> <#45606#>(add-to-address-book<#45606#> <#45607#>name<#45607#> <#45608#>phone)<#45608#> <#45609#>...)<#45609#> 
We can tell from the effect statement that the definition of <#68292#><#45613#>address-book<#45613#><#68292#> is modified in a fashion that's coherent with its purpose statement and contract.
Program Examples:
Examples are as important as ever, but formulating them has become more difficult. As before, we must develop examples that illustrate the relationship between inputs and outputs, but, because functions now have effects, we also need examples that illustrate those. Let us return to our first running example, the <#68293#><#45614#>next<#45614#><#68293#> function for traffic lights. It affects one state-variable: <#68294#><#45615#>current-color<#45615#><#68294#>. Because this variable can stand for one of three symbols, we can actually characterize all of its possible effects with examples:
<#71736#>;; if <#68295#><#45620#>current-color<#45620#><#68295#> is <#68296#><#45621#>'<#45621#><#45622#>green<#45622#><#68296#> and we evaluate <#68297#><#45623#>(next)<#45623#><#68297#>, <#71736#>
<#71737#>;; then <#68298#><#45624#>current-color<#45624#><#68298#> is <#68299#><#45625#>'<#45625#><#45626#>yellow<#45626#><#68299#> afterwards<#71737#> 
<#71738#>;; if <#68300#><#45634#>current-color<#45634#><#68300#> is <#68301#><#45635#>'<#45635#><#45636#>yellow<#45636#><#68301#> and we evaluate <#68302#><#45637#>(next)<#45637#><#68302#>, <#71738#>
<#71739#>;; then <#68303#><#45638#>current-color<#45638#><#68303#> is <#68304#><#45639#>'<#45639#><#45640#>red<#45640#><#68304#> afterwards<#71739#> 
<#71740#>;; if <#68305#><#45648#>current-color<#45648#><#68305#> is <#68306#><#45649#>'<#45649#><#45650#>red<#45650#><#68306#> and we evaluate <#68307#><#45651#>(next)<#45651#><#68307#>, <#71740#>
<#71741#>;; then <#68308#><#45652#>current-color<#45652#><#68308#> is <#68309#><#45653#>'<#45653#><#45654#>green<#45654#><#68309#> afterwards<#71741#> 
In contrast, the state variable <#68310#><#45658#>address-book<#45658#><#68310#> can stand for an infinite number of values, so it is impossible to make up a comprehensive series of examples. But it is still important to state a few, because examples make it easier to develop the function body later:
<#71742#>;; if <#68311#><#45663#>address-book<#45663#><#68311#> is <#68312#><#45664#>empty<#45664#><#68312#> and <#71742#>
<#71743#>;; we evaluate <#68313#><#45665#>(add-to-address-book<#45665#> <#45666#>'<#45666#><#45667#>Adam<#45667#> <#45668#>1)<#45668#><#68313#>, <#71743#> 
<#72260#>;; then <#68314#><#45669#>address-book<#45669#><#68314#> is <#71744#><#68315#><#45670#>(list<#45670#> <#45671#>(list<#45671#> <#45672#>'<#45672#><#45673#>Adam<#45673#> <#45674#>1))<#45674#><#68315#><#71744#> afterwards. <#72260#> 
<#72261#>;; if <#68316#><#45682#>address-book<#45682#><#68316#> is <#71745#><#68317#><#45683#>(list<#45683#> <#45684#>(list<#45684#> <#45685#>'<#45685#><#45686#>Eve<#45686#> <#45687#>2))<#45687#><#68317#><#71745#> and <#72261#>
<#71746#>;; we evaluate <#68318#><#45688#>(add-to-address-book<#45688#> <#45689#>'<#45689#><#45690#>Adam<#45690#> <#45691#>1)<#45691#><#68318#>, <#71746#> 
<#72262#>;; then <#68319#><#45692#>address-book<#45692#><#68319#> is <#71747#><#68320#><#45693#>(list<#45693#> <#45694#>(list<#45694#> <#45695#>'<#45695#><#45696#>Adam<#45696#> <#45697#>1)<#45697#> <#45698#>(list<#45698#> <#45699#>'<#45699#><#45700#>Eve<#45700#> <#45701#>2))<#45701#><#68320#><#71747#> afterwards. <#72262#> 
<#72263#>;; if <#68321#><#45709#>address-book<#45709#><#68321#> is <#71748#><#68322#><#45710#>(list<#45710#> <#45711#>E-1<#45711#> <#45712#>...<#45712#> <#45713#>E-2)<#45713#><#68322#><#71748#> and <#72263#>
<#71749#>;; we evaluate <#68323#><#45714#>(add-to-address-book<#45714#> <#45715#>'<#45715#><#45716#>Adam<#45716#> <#45717#>1)<#45717#><#68323#>, <#71749#> 
<#72264#>;; then <#68324#><#45718#>address-book<#45718#><#68324#> is <#71750#><#68325#><#45719#>(list<#45719#> <#45720#>(list<#45720#> <#45721#>'<#45721#><#45722#>Adam<#45722#> <#45723#>1)<#45723#> <#45724#>E-1<#45724#> <#45725#>...<#45725#> <#45726#>E-2)<#45726#><#68325#><#71750#> afterwards. <#72264#> 
Not surprisingly, the language of examples involves words of temporal nature. After all, assignmentss emphasize the notion of time in programming.

<#45730#>Warning<#45730#>:\ The state variable is never a parameter of a function.~<#68326#><#68326#>

The Template:
The template for state-changing functions is like that of an ordinary function, but the body should also contain <#68327#><#45733#>set!<#45733#><#68327#> expressions to specify the state variables that are to be modified:
<#45738#>(d<#45738#><#45739#>efine<#45739#> <#45740#>(fun-for-state-change<#45740#> <#45741#>x<#45741#> <#45742#>y<#45742#> <#45743#>z)<#45743#>
  <#45744#>(set!<#45744#> <#45745#>a-state-variable<#45745#> <#45746#>...))<#45746#> 
The computation of the next value for <#68328#><#45750#>a-state-variable<#45750#><#68328#> can be left to an auxiliary function, which consumes <#68329#><#45751#>x<#45751#><#68329#>, <#68330#><#45752#>y<#45752#><#68330#>, and <#68331#><#45753#>z<#45753#><#68331#>. Our two examples fit this pattern. On occasion, we should add selector and <#68332#><#45754#>cond<#45754#>-expression<#68332#>s, based on the data definitions for the function's inputs. Consider <#68333#><#45755#>next<#45755#><#68333#> again. The data definition for its input suggests a <#68334#><#45756#>cond<#45756#>-expression<#68334#>:
<#45761#>(d<#45761#><#45762#>efine<#45762#> <#45763#>(next)<#45763#>
  <#45764#>(c<#45764#><#45765#>ond<#45765#> 
    <#45766#>[<#45766#><#45767#>(symbol=?<#45767#> <#45768#>'<#45768#><#45769#>green<#45769#> <#45770#>current-color)<#45770#> <#45771#>(set!<#45771#> <#45772#>current-color<#45772#> <#45773#>...)]<#45773#> 
    <#45774#>[<#45774#><#45775#>(symbol=?<#45775#> <#45776#>'<#45776#><#45777#>yellow<#45777#> <#45778#>current-color)<#45778#> <#45779#>(set!<#45779#> <#45780#>current-color<#45780#> <#45781#>...)]<#45781#> 
    <#45782#>[<#45782#><#45783#>(symbol=?<#45783#> <#45784#>'<#45784#><#45785#>red<#45785#> <#45786#>current-color)<#45786#> <#45787#>(set!<#45787#> <#45788#>current-color<#45788#> <#45789#>...)]<#45789#><#45790#>))<#45790#> 
In this simple case, we can indeed go with either alternative and design a proper program.
The Body:
As always, the development of the full function requires a solid understanding of the examples, of how they are computed, and of the template. For functions with effects, the completion of the <#68335#><#45794#>set!<#45794#><#68335#> expression is the most demanding step. In some cases, the right-hand side involves nothing but primitive operations, the function's parameters, and the state variable (or several of them). In others, it is best to develop an auxiliary function (without effect) that consumes the current value of the state variable and the function's parameters and that produces the new value of the state variable. The function <#68336#><#45795#>add-to-address-book<#45795#><#68336#> is an example of the first kind. The right-hand side of the <#45796#>set!<#45796#>-expression\ consists of <#68337#><#45797#>address-book<#45797#><#68337#>, <#68338#><#45798#>cons<#45798#><#68338#>, <#68339#><#45799#>list<#45799#><#68339#>, and nothing else. The traffic-light example, in contrast, is a good basis to demonstrate both choices. Here is a definition that is based on the template:
<#45804#>(d<#45804#><#45805#>efine<#45805#> <#45806#>(next)<#45806#>
  <#45807#>(c<#45807#><#45808#>ond<#45808#> 
    <#45809#>[<#45809#><#45810#>(symbol=?<#45810#> <#45811#>'<#45811#><#45812#>green<#45812#> <#45813#>current-color)<#45813#> <#45814#>(set!<#45814#> <#45815#>current-color<#45815#> <#45816#>'<#45816#><#45817#>yellow)]<#45817#> 
    <#45818#>[<#45818#><#45819#>(symbol=?<#45819#> <#45820#>'<#45820#><#45821#>yellow<#45821#> <#45822#>current-color)<#45822#> <#45823#>(set!<#45823#> <#45824#>current-color<#45824#> <#45825#>'<#45825#><#45826#>red)]<#45826#> 
    <#45827#>[<#45827#><#45828#>(symbol=?<#45828#> <#45829#>'<#45829#><#45830#>red<#45830#> <#45831#>current-color)<#45831#> <#45832#>(set!<#45832#> <#45833#>current-color<#45833#> <#45834#>'<#45834#><#45835#>green)]<#45835#><#45836#>))<#45836#> 
Writing one based on an auxiliary function is also straightforward:
<#45844#>(d<#45844#><#45845#>efine<#45845#> <#45846#>(next)<#45846#>
  <#45847#>(set!<#45847#> <#45848#>current-color<#45848#> <#45849#>(next-color<#45849#> <#45850#>current-color)))<#45850#> 
For the definition of <#68340#><#45854#>next-color<#45854#><#68340#>, see page~#pgnextcolor#45855>.
Testing:
In the past, we have tested functions by translating the examples into boolean-valued expressions and by adding them to the bottom of the <#45856#>Definitions<#45856#> window. For functions with effects, we use a similar approach, but to verify that functions have the desired effect on state variables is a complex task. There are two ways to test functions with efffects. First, we can set the state variable into a desired state, apply the function, and then check whether the functions has the desired result and effect. The <#68341#><#45857#>next<#45857#><#68341#> function is a particular good one for this approach. We characterized its complete behavior with three examples. All three can be translated into <#68342#><#45858#>begin<#45858#>-expression<#68342#>s that test as suggested. Here is one example:
<#45863#>(begin<#45863#> <#45864#>(set!<#45864#> <#45865#>current-color<#45865#> <#45866#>'<#45866#><#45867#>green)<#45867#>
       <#45868#>(next)<#45868#> 
       <#45869#>(symbol=?<#45869#> <#45870#>current-color<#45870#> <#45871#>'<#45871#><#45872#>yellow))<#45872#> 
Each line sets the state variable <#68343#><#45876#>current-color<#45876#><#68343#> to the desired color, evaluates <#68344#><#45877#>(next)<#45877#><#68344#>, and then checks whether the effect is appropriate. We can also do this for the <#68345#><#45878#>add-to-address-book<#45878#><#68345#> function, though the tests are less comprehensive than those for <#68346#><#45879#>next<#45879#><#68346#>:
<#45884#>(begin<#45884#> <#45885#>(set!<#45885#> <#45886#>address-book<#45886#> <#45887#>empty)<#45887#>
       <#45888#>(add-to-address-book<#45888#> <#45889#>'<#45889#><#45890#>Adam<#45890#> <#45891#>1)<#45891#> 
       <#45892#>(equal?<#45892#> <#45893#>'<#45893#><#45894#>((Adam<#45894#> <#45895#>1))<#45895#> <#45896#>address-book))<#45896#> 
In this one test, we only check that <#68347#><#45900#>Adam<#45900#><#68347#> and <#68348#><#45901#>1<#45901#><#68348#> were properly included into the <#68349#><#45902#>empty<#45902#><#68349#> list. Second, we can capture the value of a state variable before it is tested, apply the memory-changing function, and then conduct appropriate tests. Consider the following expression:
<#45907#>(l<#45907#><#45908#>ocal<#45908#> <#45909#>([<#45909#><#45910#>define<#45910#> <#45911#>current-value-of<#45911#> <#45912#>address-book]<#45912#><#45913#>)<#45913#>
  <#45914#>(b<#45914#><#45915#>egin<#45915#> 
    <#45916#>(add-to-address-book<#45916#> <#45917#>'<#45917#><#45918#>Adam<#45918#> <#45919#>1)<#45919#> 
    <#45920#>(equal?<#45920#> <#45921#>(cons<#45921#> <#45922#>(list<#45922#> <#45923#>'<#45923#><#45924#>Adam<#45924#> <#45925#>1)<#45925#> <#45926#>current-value-of)<#45926#> <#45927#>address-book)))<#45927#> 
It defines <#68350#><#45931#>current-value-of<#45931#><#68350#> to be the value of <#68351#><#45932#>address-book<#45932#><#68351#> at the beginning of the evaluation, and at the end checks that the appropriate entry was added at the front and that nothing changed for the rest of the value. To conduct tests for functions with effects, especially tests of the second kind, it is useful to abstract the test expression into a function:
<#71751#>;; <#68352#><#45937#>test-for-address-book<#45937#> <#45938#>:<#45938#> <#45939#>symbol<#45939#> <#45940#>number<#45940#> <#45941#><#45941#><#45942#>-;SPMgt;<#45942#><#45943#><#45943#> <#45944#>boolean<#45944#><#68352#><#71751#>
<#71752#>;; to determine whether <#68353#><#45945#>add-to-address-book<#45945#><#68353#> has the appropriate<#71752#> 
<#71753#>;; effect on <#68354#><#45946#>address-book<#45946#><#68354#> and no more than that<#71753#> 
<#71754#>;; effect: same as <#68355#><#45947#>(add-to-address-book<#45947#> <#45948#>name<#45948#> <#45949#>number)<#45949#><#68355#><#71754#> 
<#45950#>(d<#45950#><#45951#>efine<#45951#> <#45952#>(test-for-address-book<#45952#> <#45953#>name<#45953#> <#45954#>number)<#45954#> 
  <#45955#>(l<#45955#><#45956#>ocal<#45956#> <#45957#>([<#45957#><#45958#>define<#45958#> <#45959#>current-value-of<#45959#> <#45960#>address-book]<#45960#><#45961#>)<#45961#> 
    <#45962#>(b<#45962#><#45963#>egin<#45963#> 
      <#45964#>(add-to-address-book<#45964#> <#45965#>name<#45965#> <#45966#>number)<#45966#> 
      <#45967#>(equal?<#45967#> <#45968#>(cons<#45968#> <#45969#>(list<#45969#> <#45970#>name<#45970#> <#45971#>number)<#45971#> <#45972#>current-value-of)<#45972#> 
              <#45973#>address-book))))<#45973#> 
Using this function, we can now easily test <#68356#><#45977#>add-to-address-book<#45977#><#68356#> several times and ensure for each test that its effects are appropriate:
 
<#45982#>(and<#45982#> <#45983#>(test-for-address-book<#45983#> <#45984#>'<#45984#><#45985#>Adam<#45985#> <#45986#>1)<#45986#> 
     <#45987#>(test-for-address-book<#45987#> <#45988#>'<#45988#><#45989#>Eve<#45989#> <#45990#>2)<#45990#> 
     <#45991#>(test-for-address-book<#45991#> <#45992#>'<#45992#><#45993#>Chris<#45993#> <#45994#>6145384))<#45994#> 
The <#68357#><#45998#>and<#45998#>-expression<#68357#> guarantees that the test expressions are evaluated in order and that all of them produce <#68358#><#45999#>true<#45999#><#68358#>.
Future Reuse:
Once we have a complete, tested program, we should remember its existence, <#46000#>what<#46000#> it computes, and what its <#46001#>effects<#46001#> are. We do not, however, need to remember <#46002#>how<#46002#> it computes. If we encounter a situation that calls for the same computation and the same effects, we can reuse the program as if it were a primitive operation. <#46003#>Warning<#46003#>: In the presence of effects, it is much more difficult to reuse a function than in the world of algebraic programs.
Figures~#figsvrecipe1example#46005> and~#figsvrecipe2example#46006> summarize our two running examples; the header in the first one is omitted because it is useless for the purpose and effect statements in this particular case.
<#71755#>;; <#46011#>Data Def.<#46011#>: lists of arbitrary length: <#68359#><#46012#>(listof<#46012#> <#46013#>X)<#46013#><#68359#>, lists of two items: <#68360#><#46014#>(list<#46014#> <#46015#>Y<#46015#> <#46016#>Z)<#46016#><#68360#><#71755#>
<#68361#>;; <#46017#>State Variable<#46017#>: <#68361#> 
<#71756#>;; <#68362#><#46018#>address-book<#46018#> <#46019#>:<#46019#> <#46020#>(listof<#46020#> <#46021#>(list<#46021#> <#46022#>symbol<#46022#> <#46023#>number))<#46023#><#68362#><#71756#> 
<#46024#>;; to keep track of pairs of names and phone numbers<#46024#> 
<#46025#>(define<#46025#> <#46026#>address-book<#46026#> <#46027#>empty)<#46027#> 
<#71757#>;; <#46028#>Contract<#46028#>: <#68363#><#46029#>add-to-address-book<#46029#> <#46030#>:<#46030#> <#46031#>symbol<#46031#> <#46032#>number<#46032#> <#46033#><#46033#><#46034#>-;SPMgt;<#46034#><#46035#><#46035#> <#46036#>void<#46036#><#68363#><#71757#> 
<#71758#>;; <#46037#>Purpose<#46037#>: the function always produces <#68364#><#46038#>(<#46038#><#46039#>void<#46039#><#46040#>)<#46040#><#68364#><#71758#> 
<#71759#>;; <#46041#>Effect<#46041#>: to add <#68365#><#46042#>(list<#46042#> <#46043#>name<#46043#> <#46044#>phone)<#46044#><#68365#> to the front of <#68366#><#46045#>address-book<#46045#><#68366#><#71759#> 
<#68367#>;; <#46046#>Header<#46046#>: <#68367#> 
<#71760#>;; <#68368#><#46047#>(define<#46047#> <#46048#>(add-to-address-book<#46048#> <#46049#>name<#46049#> <#46050#>phone)<#46050#> <#46051#>...)<#46051#><#68368#><#71760#> 
<#68369#>;; <#46052#>Examples<#46052#>: <#68369#> 
<#72265#>;; if <#68370#><#46053#>address-book<#46053#><#68370#> is <#71761#><#68371#><#46054#>empty<#46054#><#68371#><#71761#> and we evaluate<#72265#> 
<#72266#>;; <#68372#><#46055#>(add-to-address-book<#46055#> <#46056#>'<#46056#><#46057#>Adam<#46057#> <#46058#>1)<#46058#><#68372#>, <#68373#><#46059#>address-book<#46059#><#68373#> is <#71762#><#68374#><#46060#>(list<#46060#> <#46061#>(list<#46061#> <#46062#>'<#46062#><#46063#>Adam<#46063#> <#46064#>1))<#46064#><#68374#><#71762#>.<#72266#> 
<#72267#>;; if <#68375#><#46065#>address-book<#46065#><#68375#> is <#71763#><#68376#><#46066#>(list<#46066#> <#46067#>(list<#46067#> <#46068#>'<#46068#><#46069#>Eve<#46069#> <#46070#>2))<#46070#><#68376#><#71763#> and we evaluate<#72267#> 
<#72268#>;; <#68377#><#46071#>(add-to-address-book<#46071#> <#46072#>'<#46072#><#46073#>Adam<#46073#> <#46074#>1)<#46074#><#68377#>, <#68378#><#46075#>address-book<#46075#><#68378#> is <#71764#><#68379#><#46076#>(list<#46076#> <#46077#>(list<#46077#> <#46078#>'<#46078#><#46079#>Adam<#46079#> <#46080#>1)<#46080#> <#46081#>(list<#46081#> <#46082#>'<#46082#><#46083#>Eve<#46083#> <#46084#>2))<#46084#><#68379#><#71764#>.<#72268#> 
<#72269#>;; if <#68380#><#46085#>address-book<#46085#><#68380#> is <#71765#><#68381#><#46086#>(list<#46086#> <#46087#>E-1<#46087#> <#46088#>...<#46088#> <#46089#>E-2)<#46089#><#68381#><#71765#> and we evaluate<#72269#> 
<#72270#>;; <#68382#><#46090#>(add-to-address-book<#46090#> <#46091#>'<#46091#><#46092#>Adam<#46092#> <#46093#>1)<#46093#><#68382#>, <#68383#><#46094#>address-book<#46094#><#68383#> is <#71766#><#68384#><#46095#>(list<#46095#> <#46096#>(list<#46096#> <#46097#>'<#46097#><#46098#>Adam<#46098#> <#46099#>1)<#46099#> <#46100#>E-1<#46100#> <#46101#>...<#46101#> <#46102#>E-2)<#46102#><#68384#><#71766#>.<#72270#> 
<#68385#>;; <#46103#>Template<#46103#>: omitted<#68385#> 
<#68386#>;; <#46104#>Definition<#46104#>:<#68386#> 
<#46105#>(d<#46105#><#46106#>efine<#46106#> <#46107#>(add-to-address-book<#46107#> <#46108#>name<#46108#> <#46109#>phone)<#46109#> 
  <#46110#>(set!<#46110#> <#46111#>address-book<#46111#> <#46112#>(cons<#46112#> <#46113#>(list<#46113#> <#46114#>name<#46114#> <#46115#>phone)<#46115#> <#46116#>address-book)))<#46116#> 
  
<#68387#>;; <#46117#>Tests<#46117#>:<#68387#> 
<#46118#>(begin<#46118#> <#46119#>(set!<#46119#> <#46120#>address-book<#46120#> <#46121#>empty)<#46121#> 
       <#46122#>(add-to-address-book<#46122#> <#46123#>'<#46123#><#46124#>Adam<#46124#> <#46125#>1)<#46125#> 
       <#46126#>(equal?<#46126#> <#46127#>'<#46127#><#46128#>((Adam<#46128#> <#46129#>1))<#46129#> <#46130#>address-book))<#46130#> 
<#46134#>Figure: The design recipe for state variables: a second example<#46134#>

<#46138#>Exercise 36.4.1<#46138#> Modify the traffic light program in figure~#figsvrecipe1example#46140> to draw the current state of the traffic light into a canvas. Start by adding the initializer. Use the solutions for section~#sectrafficdraw#46141>.~ external Solution<#68388#><#68388#> <#46147#>Exercise 36.4.2<#46147#> Modify the phone book program in figure~#figsvrecipe2example#46149> so that it offers a graphical user interface. Start by adding the initializer. Use the solution of exercise~#exsetpbgui#46150>.~ external Solution<#68389#><#68389#>