file no10
<#71195#>;; <#64733#><#24614#>mult10<#24614#> <#24615#>:<#24615#> <#24616#>list-of-digits<#24616#> <#24617#><#24617#><#24618#>-;SPMgt;<#24618#><#24619#><#24619#> <#24620#>list-of-numbers<#24620#><#64733#><#71195#>
<#71196#>;; to create a list of numbers by multiplying each digit on <#64734#><#24621#>alod<#24621#><#64734#> <#71196#>
<#71197#>;; by <#64735#><#24622#>(expt<#24622#> <#24623#>10<#24623#> <#24624#>p)<#24624#><#64735#> where <#64736#><#24625#>p<#24625#><#64736#> is the number of digits that follow<#71197#>
<#24626#>(d<#24626#><#24627#>efine<#24627#> <#24628#>(mult10<#24628#> <#24629#>alod)<#24629#>
<#24630#>(c<#24630#><#24631#>ond<#24631#>
<#24632#>[<#24632#><#24633#>(empty?<#24633#> <#24634#>alod)<#24634#> <#24635#>0]<#24635#>
<#24636#>[<#24636#><#24637#>else<#24637#> <#24638#>(c<#24638#><#24639#>ons<#24639#> <#24640#>(*<#24640#> <#24641#>(expt<#24641#> <#24642#>10<#24642#> <#24643#>(length<#24643#> <#24644#>(rest<#24644#> <#24645#>alod)))<#24645#> <#24646#>(first<#24646#> <#24647#>alod))<#24647#>
<#24648#>(mult10<#24648#> <#24649#>(rest<#24649#> <#24650#>alod)))]<#24650#><#24651#>))<#24651#>
Here are some sample application:
<#24659#>;SPMgt;<#24659#> <#24660#>(mult10<#24660#> <#24661#>(list<#24661#> <#24662#>1<#24662#> <#24663#>2<#24663#> <#24664#>3))<#24664#> <#24665#>(list<#24665#> <#24666#>100<#24666#> <#24667#>20<#24667#> <#24668#>3)<#24668#> <#24669#>;SPMgt;<#24669#> <#24670#>(mult10<#24670#> <#24671#>(list<#24671#> <#24672#>9<#24672#> <#24673#>8<#24673#> <#24674#>7))<#24674#> <#24675#>(list<#24675#> <#24676#>900<#24676#> <#24677#>80<#24677#> <#24678#>7)<#24678#>Clearly, the function could be used to convert a list of digits into a number. A small problem with the definition of <#64737#><#24682#>mult10<#24682#><#64737#> is the computation of the first item of the result in the second clause. It is a large expression and doesn't quite correspond to the purpose statement. By using a <#64738#><#24683#>local<#24683#>-expression<#64738#> in the second clause, we can introduce names for some intermediate values in the computation of the answer:
<#71198#>;; <#64739#><#24688#>mult10<#24688#> <#24689#>:<#24689#> <#24690#>list-of-digits<#24690#> <#24691#><#24691#><#24692#>-;SPMgt;<#24692#><#24693#><#24693#> <#24694#>list-of-numbers<#24694#><#64739#><#71198#>
<#71199#>;; to create a list of numbers by multiplying each digit on <#64740#><#24695#>alod<#24695#><#64740#> <#71199#>
<#71200#>;; by <#64741#><#24696#>(expt<#24696#> <#24697#>10<#24697#> <#24698#>p)<#24698#><#64741#> where <#64742#><#24699#>p<#24699#><#64742#> is the number of digits that follow<#71200#>
<#24700#>(d<#24700#><#24701#>efine<#24701#> <#24702#>(mult10<#24702#> <#24703#>alon)<#24703#>
<#24704#>(c<#24704#><#24705#>ond<#24705#>
<#24706#>[<#24706#><#24707#>(empty?<#24707#> <#24708#>alon)<#24708#> <#24709#>empty]<#24709#>
<#24710#>[<#24710#><#24711#>else<#24711#> <#24712#>(l<#24712#><#24713#>ocal<#24713#> <#24714#>(<#24714#><#24715#>(define<#24715#> <#24716#>a-digit<#24716#> <#24717#>(first<#24717#> <#24718#>alon))<#24718#>
<#24719#>(define<#24719#> <#24720#>p<#24720#> <#24721#>(length<#24721#> <#24722#>(rest<#24722#> <#24723#>alon))))<#24723#>
<#24724#>;; ------------------------------------------------------<#24724#>
<#24725#>(cons<#24725#> <#24726#>(*<#24726#> <#24727#>(expt<#24727#> <#24728#>10<#24728#> <#24729#>p)<#24729#> <#24730#>a-digit)<#24730#> <#24731#>(mult10<#24731#> <#24732#>(rest<#24732#> <#24733#>alon))))]<#24733#><#24734#>))<#24734#>
The use of names helps us understand the expression when we read the
definition again because we can study one <#64743#><#24738#>local<#24738#><#64743#>-definition at a
time.
The use of <#64744#><#24739#>local<#24739#><#64744#> for such cases is most appropriate when a value
is computed twice as, for example, the expression <#64745#><#24740#>(rest<#24740#>\ <#24741#>alon)<#24741#><#64745#> in
<#64746#><#24742#>mult10<#24742#><#64746#>. By introducing names for repeated expressions, we might
also avoid some (small) effort on DrScheme's side:
<#24747#>(d<#24747#><#24748#>efine<#24748#> <#24749#>(mult10<#24749#> <#24750#>alon)<#24750#>
<#24751#>(c<#24751#><#24752#>ond<#24752#>
<#24753#>[<#24753#><#24754#>(empty?<#24754#> <#24755#>alon)<#24755#> <#24756#>empty]<#24756#>
<#24757#>[<#24757#><#24758#>else<#24758#> <#24759#>(l<#24759#><#24760#>ocal<#24760#> <#24761#>(<#24761#><#24762#>(define<#24762#> <#24763#>a-digit<#24763#> <#24764#>(first<#24764#> <#24765#>alon))<#24765#>
<#24766#>(define<#24766#> <#24767#>the-rest<#24767#> <#24768#>(rest<#24768#> <#24769#>alon))<#24769#>
<#24770#>(define<#24770#> <#24771#>p<#24771#> <#24772#>(length<#24772#> <#24773#>the-rest)))<#24773#>
<#24774#>;; ------------------------------------------------------<#24774#>
<#24775#>(cons<#24775#> <#24776#>(*<#24776#> <#24777#>(expt<#24777#> <#24778#>10<#24778#> <#24779#>p)<#24779#> <#24780#>a-digit)<#24780#> <#24781#>(mult10<#24781#> <#24782#>the-rest)))]<#24782#><#24783#>))<#24783#>
For the programs that we have developed, this third usage of <#64747#><#24787#>local<#24787#><#64747#>
is hardly ever useful. An auxiliary function is almost always better. We
will, however, encounter many different styles of functions in the
remaining parts of the book and with them the opportunity, and sometimes
the necessity, to use <#64748#><#24788#>local<#24788#>-expression<#64748#>s like the one for
<#64749#><#24789#>mult10<#24789#><#64749#>.
<#71201#>;; <#64750#><#24798#>extract1<#24798#> <#24799#>:<#24799#> <#24800#>inventory<#24800#> <#24801#><#24801#><#24802#>-;SPMgt;<#24802#><#24803#><#24803#> <#24804#>inventory<#24804#><#64750#><#71201#>
<#71202#>;; to create an <#64751#><#24805#>inventory<#24805#><#64751#> from <#64752#><#24806#>an-inv<#24806#><#64752#> for all<#71202#>
<#24807#>;; those items that cost less than $1<#24807#>
<#24808#>(d<#24808#><#24809#>efine<#24809#> <#24810#>(extract1<#24810#> <#24811#>an-inv)<#24811#>
<#24812#>(c<#24812#><#24813#>ond<#24813#>
<#24814#>[<#24814#><#24815#>(empty?<#24815#> <#24816#>an-inv)<#24816#> <#24817#>empty]<#24817#>
<#24818#>[<#24818#><#24819#>else<#24819#> <#24820#>(c<#24820#><#24821#>ond<#24821#>
<#24822#>[<#24822#><#24823#>(;SPMlt;=<#24823#> <#24824#>(ir-price<#24824#> <#24825#>(first<#24825#> <#24826#>an-inv))<#24826#> <#24827#>1.00)<#24827#>
<#24828#>(cons<#24828#> <#24829#>(first<#24829#> <#24830#>an-inv)<#24830#> <#24831#>(extract1<#24831#> <#24832#>(rest<#24832#> <#24833#>an-inv)))]<#24833#>
<#24834#>[<#24834#><#24835#>else<#24835#> <#24836#>(extract1<#24836#> <#24837#>(rest<#24837#> <#24838#>an-inv))]<#24838#><#24839#>)]<#24839#><#24840#>))<#24840#>
Both clauses in the nested <#64753#><#24844#>cond<#24844#>-expression<#64753#> extract the first item
from <#64754#><#24845#>an-inv<#24845#><#64754#> and both compute <#64755#><#24846#>(extract1<#24846#>\ <#24847#>(rest<#24847#>\ <#24848#>an-inv))<#24848#><#64755#>.
Introduce a <#64756#><#24849#>local<#24849#>-expression<#64756#> for these
expressions.~