file no8 file no8

Pragmatics of local, Part 1

The most important use of <#64619#><#23856#>local<#23856#>-expression<#64619#>s is to group collections of functions that serve one purpose. Consider for an example the definitions for our sort function from section~#secsortI#23857>:
<#71183#>;; <#64620#><#23862#>sort<#23862#> <#23863#>:<#23863#> <#23864#>list-of-numbers<#23864#> <#23865#><#23865#><#23866#>-;SPMgt;<#23866#><#23867#><#23867#> <#23868#>list-of-numbers<#23868#><#64620#><#71183#>
<#23869#>(d<#23869#><#23870#>efine<#23870#> <#23871#>(sort<#23871#> <#23872#>alon)<#23872#> 
  <#23873#>(c<#23873#><#23874#>ond<#23874#> 
    <#23875#>[<#23875#><#23876#>(empty?<#23876#> <#23877#>alon)<#23877#> <#23878#>empty]<#23878#> 
    <#23879#>[<#23879#><#23880#>(cons?<#23880#> <#23881#>alon)<#23881#> <#23882#>(insert<#23882#> <#23883#>(first<#23883#> <#23884#>alon)<#23884#> <#23885#>(sort<#23885#> <#23886#>(rest<#23886#> <#23887#>alon)))]<#23887#><#23888#>))<#23888#> 
<#71184#>;; <#64621#><#23889#>insert<#23889#> <#23890#>:<#23890#> <#23891#>number<#23891#> <#23892#>list-of-numbers<#23892#> <#23893#>(sorted)<#23893#> <#23894#><#23894#><#23895#>-;SPMgt;<#23895#><#23896#><#23896#> <#23897#>list-of-numbers<#23897#><#64621#><#71184#> 
<#23898#>(d<#23898#><#23899#>efine<#23899#> <#23900#>(insert<#23900#> <#23901#>an<#23901#> <#23902#>alon)<#23902#> 
  <#23903#>(c<#23903#><#23904#>ond<#23904#> 
    <#23905#>[<#23905#><#23906#>(empty?<#23906#> <#23907#>alon)<#23907#> <#23908#>(list<#23908#> <#23909#>an)]<#23909#> 
    <#23910#>[<#23910#><#23911#>else<#23911#> <#23912#>(c<#23912#><#23913#>ond<#23913#> 
            <#23914#>[<#23914#><#23915#>(;SPMgt;<#23915#> <#23916#>an<#23916#> <#23917#>(first<#23917#> <#23918#>alon))<#23918#> <#23919#>(cons<#23919#> <#23920#>an<#23920#> <#23921#>alon)]<#23921#> 
            <#23922#>[<#23922#><#23923#>else<#23923#> <#23924#>(cons<#23924#> <#23925#>(first<#23925#> <#23926#>alon)<#23926#> <#23927#>(insert<#23927#> <#23928#>an<#23928#> <#23929#>(rest<#23929#> <#23930#>alon)))]<#23930#><#23931#>)]<#23931#><#23932#>))<#23932#> 
The first definition defines <#64622#><#23936#>sort<#23936#><#64622#> per se, the second one defines an auxiliary function that inserts a number into a sorted list of numbers. The first one uses the second one to construct the result from a natural recursion, a sorted version of the rest of the list, and the first item. The two functions together form the program that sorts a list of numbers. To indicate this intimate relationship between the functions, we can, and should, use a <#64623#><#23937#>local<#23937#>-expression<#64623#>. Specifically, we define a program <#64624#><#23938#>sort<#23938#><#64624#> that immediately introduces the two functions as auxiliary definitions:
<#71185#>;; <#64625#><#23943#>sort<#23943#> <#23944#>:<#23944#> <#23945#>list-of-numbers<#23945#> <#23946#><#23946#><#23947#>-;SPMgt;<#23947#><#23948#><#23948#> <#23949#>list-of-numbers<#23949#><#64625#><#71185#>
<#23950#>(d<#23950#><#23951#>efine<#23951#> <#23952#>(sort<#23952#> <#23953#>alon)<#23953#> 
  <#23954#>(l<#23954#><#23955#>ocal<#23955#> <#23956#>(<#23956#><#23957#>(d<#23957#><#23958#>efine<#23958#> <#23959#>(sort<#23959#> <#23960#>alon)<#23960#> 
            <#23961#>(c<#23961#><#23962#>ond<#23962#> 
              <#23963#>[<#23963#><#23964#>(empty?<#23964#> <#23965#>alon)<#23965#> <#23966#>empty]<#23966#> 
              <#23967#>[<#23967#><#23968#>(cons?<#23968#> <#23969#>alon)<#23969#> <#23970#>(insert<#23970#> <#23971#>(first<#23971#> <#23972#>alon)<#23972#> <#23973#>(sort<#23973#> <#23974#>(rest<#23974#> <#23975#>alon)))]<#23975#><#23976#>))<#23976#> 
          <#23977#>(d<#23977#><#23978#>efine<#23978#> <#23979#>(insert<#23979#> <#23980#>an<#23980#> <#23981#>alon)<#23981#> 
            <#23982#>(c<#23982#><#23983#>ond<#23983#> 
              <#23984#>[<#23984#><#23985#>(empty?<#23985#> <#23986#>alon)<#23986#> <#23987#>(list<#23987#> <#23988#>an)]<#23988#> 
              <#23989#>[<#23989#><#23990#>else<#23990#> <#23991#>(c<#23991#><#23992#>ond<#23992#> 
                      <#23993#>[<#23993#><#23994#>(;SPMgt;<#23994#> <#23995#>an<#23995#> <#23996#>(first<#23996#> <#23997#>alon))<#23997#> <#23998#>(cons<#23998#> <#23999#>an<#23999#> <#24000#>alon)]<#24000#> 
                      <#24001#>[<#24001#><#24002#>else<#24002#> <#24003#>(cons<#24003#> <#24004#>(first<#24004#> <#24005#>alon)<#24005#> <#24006#>(insert<#24006#> <#24007#>an<#24007#> <#24008#>(rest<#24008#> <#24009#>alon)))]<#24009#><#24010#>)]<#24010#><#24011#>)))<#24011#> 
    <#24012#>(sort<#24012#> <#24013#>alon)))<#24013#> 
Here the body of <#64626#><#24017#>local<#24017#>-expression<#64626#>s simply passes on the argument to the locally defined function <#64627#><#24018#>sort<#24018#><#64627#>. rawhtml27 Guideline on the Use of local rawhtml28 Develop a function following the design recipes. If the function requires the use of auxiliary definitions, group them in a <#64628#><#24019#>local<#24019#>-expression<#64628#> and put the <#64629#><#24020#>local<#24020#>-expression<#64629#> into a new function definition. The body of the <#64630#><#24021#>local<#24021#><#64630#> should apply the main function to the arguments of the newly defined function. rawhtml29
<#24024#>Exercise 18.1.6<#24024#> Evaluate <#64631#><#24026#>(sort<#24026#>\ <#24027#>(list<#24027#>\ <#24028#>2<#24028#>\ <#24029#>1<#24029#>\ <#24030#>3))<#24030#><#64631#> by hand until the locally defined <#64632#><#24031#>sort<#24031#><#64632#> function is used. Do the same for <#64633#><#24032#>(equal?<#24032#>\ <#24033#>(sort<#24033#>\ <#24034#>(list<#24034#><#24035#> <#24035#><#24036#>1))<#24036#>\ <#24037#>(sort<#24037#>\ <#24038#>(list<#24038#>\ <#24039#>2)))<#24039#><#64633#>.~ external Solution<#64634#><#64634#> <#24045#>Exercise 18.1.7<#24045#> Use a <#64635#><#24047#>local<#24047#><#64635#> expression to organize the functions for moving pictures from section~#secmovefig#24048>.~ external Solution<#64636#><#64636#> <#24054#>Exercise 18.1.8<#24054#> Use a <#64637#><#24056#>local<#24056#><#64637#> expression to organize the functions for drawing a polygon in figure~#figdrawpoly#24057>.~ external Solution<#64638#><#64638#> <#24063#>Exercise 18.1.9<#24063#> Use a <#64639#><#24065#>local<#24065#><#64639#> expression to organize the functions for rearranging words from section~#secpermute#24066>.~ external Solution<#64640#><#64640#> <#24072#>Exercise 18.1.10<#24072#> Use a <#64641#><#24074#>local<#24074#><#64641#> expression to organize the functions for finding blue eyed descendants from section~#secmutrefdd#24075>.~ external Solution<#64642#><#64642#>