file no6 file no6

Syntax of local

A <#64536#><#22686#>local<#22686#>-expression<#64536#> is another form of expression:

#tabular22688#

As usual, <#71180#><#64541#>;SPMlt;<#22697#>def-1<#22697#>;SPMgt;<#64541#><#71180#> ...\ <#71181#><#64542#>;SPMlt;<#22698#>def-n<#22698#>;SPMgt;<#64542#><#71181#> is an arbitrarily long sequence of definitions (see figure~#figdefsinscheme#22699>) and <#71182#><#64543#>;SPMlt;<#22700#>exp<#22700#>;SPMgt;<#64543#><#71182#> is an arbitrary expression. In other words, a <#64544#><#22701#>local<#22701#>-expression<#64544#> consists of the keyword <#64545#><#22702#>local<#22702#><#64545#>, followed by a sequence of definitions grouped with <#64546#><#22703#>(<#22703#><#64546#> and <#64547#><#22704#>)<#22704#><#64547#>, followed by an expression. The keyword <#22705#>local<#22705#> distinguishes this new class of expressions from other expressions, just like <#64548#><#22706#>cond<#22706#><#64548#> distinguishes conditional expressions from applications. The parenthesized sequence that follows <#64549#><#22707#>local<#22707#><#64549#> is referred to as the <#64550#><#22708#>LOCAL DEFINITION<#22708#><#64550#>. The left-hand sides of the definitions are called the <#22709#>locally defined<#22709#> variables, functions, or structures. All those in the <#22710#>Definitions<#22710#> window are called <#64551#><#22711#>TOP-LEVEL DEFINITIONS<#22711#><#64551#>. Each name may occur at most once on the left-hand side, be it in a variable definition or a function definition. The expression in each definition is called the <#22712#>right-hand<#22712#> side. The expression that follows the definitions is the <#22713#>body<#22713#> of the <#64552#><#22714#>local<#22714#>-expression<#64552#>. Let us take a look at an example:

<#22719#>(l<#22719#><#22720#>ocal<#22720#> <#22721#>(<#22721#><#22722#>(define<#22722#> <#22723#>(f<#22723#> <#22724#>x)<#22724#> <#22725#>(+<#22725#> <#22726#>x<#22726#> <#22727#>5))<#22727#>
        <#22728#>(d<#22728#><#22729#>efine<#22729#> <#22730#>(g<#22730#> <#22731#>alon)<#22731#> 
          <#22732#>(c<#22732#><#22733#>ond<#22733#> 
            <#22734#>[<#22734#><#22735#>(empty?<#22735#> <#22736#>alon)<#22736#> <#22737#>empty]<#22737#> 
            <#22738#>[<#22738#><#22739#>else<#22739#> <#22740#>(cons<#22740#> <#22741#>(f<#22741#> <#22742#>(first<#22742#> <#22743#>alon))<#22743#> <#22744#>(g<#22744#> <#22745#>(rest<#22745#> <#22746#>alon)))]<#22746#><#22747#>)))<#22747#> 
  <#22748#>(g<#22748#> <#22749#>(list<#22749#> <#22750#>1<#22750#> <#22751#>2<#22751#> <#22752#>3)))<#22752#> 
The locally defined functions are <#64553#><#22756#>f<#22756#><#64553#> and <#64554#><#22757#>g<#22757#><#64554#>. The right-hand side of the first function definition is <#64555#><#22758#>(+<#22758#>\ <#22759#>x<#22759#>\ <#22760#>5)<#22760#><#64555#>; the second one is
<#22765#>(c<#22765#><#22766#>ond<#22766#>
  <#22767#>[<#22767#><#22768#>(empty?<#22768#> <#22769#>alon)<#22769#> <#22770#>empty]<#22770#> 
  <#22771#>[<#22771#><#22772#>else<#22772#> <#22773#>(cons<#22773#> <#22774#>(f<#22774#> <#22775#>(first<#22775#> <#22776#>alon))<#22776#> <#22777#>(g<#22777#> <#22778#>(rest<#22778#> <#22779#>alon)))]<#22779#><#22780#>)<#22780#> 
Finally, the body of the <#64556#><#22784#>local<#22784#>-expression<#64556#> is <#64557#><#22785#>(g<#22785#>\ <#22786#>(list<#22786#>\ <#22787#>1<#22787#>\ <#22788#>2<#22788#>\ <#22789#>3))<#22789#><#64557#>.
<#22792#>Exercise 18.1.1<#22792#> Circle the locally defined variables and functions in red, the right-hand sides in green, and the body of the following <#22794#>local<#22794#>-expression\ in blue:
<#22799#>1.<#22799#> <#22800#>(local<#22800#> <#22801#>((define<#22801#> <#22802#>x<#22802#> <#22803#>(*<#22803#> <#22804#>y<#22804#> <#22805#>3)))<#22805#>
         <#22806#>(*<#22806#> <#22807#>x<#22807#> <#22808#>x))<#22808#> 
<#22816#>2.<#22816#> <#22817#>(local<#22817#> <#22818#>((def<#22818#><#22819#>in<#22819#><#22820#>e<#22820#> <#22821#>(odd<#22821#> <#22822#>an)<#22822#>
                 <#22823#>(c<#22823#><#22824#>ond<#22824#> 
                   <#22825#>[<#22825#><#22826#>(zero?<#22826#> <#22827#>an)<#22827#> <#22828#>false<#22828#><#22829#>]<#22829#> 
                   <#22830#>[<#22830#><#22831#>else<#22831#> <#22832#>(even<#22832#> <#22833#>(sub1<#22833#> <#22834#>an))]<#22834#><#22835#>))<#22835#> 
               <#22836#>(d<#22836#><#22837#>efine<#22837#> <#22838#>(even<#22838#> <#22839#>an)<#22839#> 
                 <#22840#>(c<#22840#><#22841#>ond<#22841#> 
                   <#22842#>[<#22842#><#22843#>(zero?<#22843#> <#22844#>an)<#22844#> <#22845#>true<#22845#><#22846#>]<#22846#> 
                   <#22847#>[<#22847#><#22848#>else<#22848#> <#22849#>(odd<#22849#> <#22850#>(sub1<#22850#> <#22851#>an))]<#22851#><#22852#>)))<#22852#> 
         <#22853#>(even<#22853#> <#22854#>a-nat-num))<#22854#> 
<#22862#>3.<#22862#> <#22863#>(local<#22863#> <#22864#>((def<#22864#><#22865#>ine<#22865#> <#22866#>(f<#22866#> <#22867#>x)<#22867#> <#22868#>(g<#22868#> <#22869#>x<#22869#> <#22870#>(+<#22870#> <#22871#>x<#22871#> <#22872#>1)))<#22872#>
               <#22873#>(define<#22873#> <#22874#>(g<#22874#> <#22875#>x<#22875#> <#22876#>y)<#22876#> <#22877#>(f<#22877#> <#22878#>(+<#22878#> <#22879#>x<#22879#> <#22880#>y))))<#22880#> 
         <#22881#>(+<#22881#> <#22882#>(f<#22882#> <#22883#>10)<#22883#> <#22884#>(g<#22884#> <#22885#>10<#22885#> <#22886#>20)))<#22886#> 
external Solution<#64558#><#64558#> <#22895#>Exercise 18.1.2<#22895#> The following phrases are <#22897#>not<#22897#> syntactically legal:
<#22902#>1.<#22902#> <#22903#>(local<#22903#> <#22904#>((def<#22904#><#22905#>ine<#22905#> <#22906#>x<#22906#> <#22907#>10)<#22907#>
               <#22908#>(y<#22908#> <#22909#>(+<#22909#> <#22910#>x<#22910#> <#22911#>x)))<#22911#> 
         <#22912#>y)<#22912#> 
<#22920#>2.<#22920#> <#22921#>(local<#22921#> <#22922#>((def<#22922#><#22923#>ine<#22923#> <#22924#>(f<#22924#> <#22925#>x)<#22925#> <#22926#>(+<#22926#> <#22927#>(*<#22927#> <#22928#>x<#22928#> <#22929#>x)<#22929#> <#22930#>(*<#22930#> <#22931#>3<#22931#> <#22932#>x)<#22932#> <#22933#>15))<#22933#>
               <#22934#>(define<#22934#> <#22935#>x<#22935#> <#22936#>100)<#22936#> 
               <#22937#>(define<#22937#> <#22938#>f@<#22938#><#22939#>100<#22939#> <#22940#>(f<#22940#> <#22941#>x)))<#22941#> 
         <#22942#>f@<#22942#><#22943#>100<#22943#> <#22944#>x)<#22944#> 
<#22952#>3.<#22952#> <#22953#>(local<#22953#> <#22954#>((def<#22954#><#22955#>ine<#22955#> <#22956#>(f<#22956#> <#22957#>x)<#22957#> <#22958#>(+<#22958#> <#22959#>(*<#22959#> <#22960#>x<#22960#> <#22961#>x)<#22961#> <#22962#>(*<#22962#> <#22963#>3<#22963#> <#22964#>x)<#22964#> <#22965#>14))<#22965#>
               <#22966#>(define<#22966#> <#22967#>x<#22967#> <#22968#>100)<#22968#> 
               <#22969#>(define<#22969#> <#22970#>f<#22970#> <#22971#>(f<#22971#> <#22972#>x)))<#22972#> 
         <#22973#>f)<#22973#> 
Explain why! external Solution<#64559#><#64559#> <#22982#>Exercise 18.1.3<#22982#> Determine which of the following definitions or expressions is legal and which one is not:
<#22988#>1.<#22988#> <#22989#>(defin<#22989#><#22990#>e<#22990#> <#22991#>A-CONSTANT<#22991#>
         <#22992#>(n<#22992#><#22993#>ot<#22993#> 
           <#22994#>(l<#22994#><#22995#>ocal<#22995#> <#22996#>(<#22996#><#22997#>(d<#22997#><#22998#>efine<#22998#> <#22999#>(odd<#22999#> <#23000#>an)<#23000#> 
                     <#23001#>(c<#23001#><#23002#>ond<#23002#> 
                       <#23003#>[<#23003#><#23004#>(=<#23004#> <#23005#>an<#23005#> <#23006#>0)<#23006#> <#23007#>false<#23007#><#23008#>]<#23008#> 
                       <#23009#>[<#23009#><#23010#>else<#23010#> <#23011#>(even<#23011#> <#23012#>(-<#23012#> <#23013#>an<#23013#> <#23014#>1))]<#23014#><#23015#>))<#23015#> 
                   <#23016#>(d<#23016#><#23017#>efine<#23017#> <#23018#>(even<#23018#> <#23019#>an)<#23019#> 
                     <#23020#>(c<#23020#><#23021#>ond<#23021#> 
                       <#23022#>[<#23022#><#23023#>(=<#23023#> <#23024#>an<#23024#> <#23025#>0)<#23025#> <#23026#>true<#23026#><#23027#>]<#23027#> 
                       <#23028#>[<#23028#><#23029#>else<#23029#> <#23030#>(odd<#23030#> <#23031#>(-<#23031#> <#23032#>an<#23032#> <#23033#>1))]<#23033#><#23034#>)))<#23034#> 
             <#23035#>(even<#23035#> <#23036#>a-nat-num))))<#23036#> 
<#23044#>2.<#23044#> <#23045#>(+<#23045#> <#23046#>(loc<#23046#><#23047#>al<#23047#> <#23048#>((def<#23048#><#23049#>ine<#23049#> <#23050#>(f<#23050#> <#23051#>x)<#23051#> <#23052#>(+<#23052#> <#23053#>(*<#23053#> <#23054#>x<#23054#> <#23055#>x)<#23055#> <#23056#>(*<#23056#> <#23057#>3<#23057#> <#23058#>x)<#23058#> <#23059#>15))<#23059#>
                  <#23060#>(define<#23060#> <#23061#>x<#23061#> <#23062#>100)<#23062#> 
                  <#23063#>(define<#23063#> <#23064#>f@<#23064#><#23065#>100<#23065#> <#23066#>(f<#23066#> <#23067#>x)))<#23067#> 
            <#23068#>f@<#23068#><#23069#>100)<#23069#> 
          <#23070#>1000)<#23070#> 
<#23078#>3.<#23078#> <#23079#>(local<#23079#> <#23080#>((def<#23080#><#23081#>ine<#23081#> <#23082#>CONST<#23082#> <#23083#>100)<#23083#>
               <#23084#>(define<#23084#> <#23085#>f<#23085#> <#23086#>x<#23086#> <#23087#>(+<#23087#> <#23088#>x<#23088#> <#23089#>CONST)))<#23089#> 
         <#23090#>(define<#23090#> <#23091#>(g<#23091#> <#23092#>x<#23092#> <#23093#>y<#23093#> <#23094#>z)<#23094#> <#23095#>(f<#23095#> <#23096#>(+<#23096#> <#23097#>x<#23097#> <#23098#>(*<#23098#> <#23099#>y<#23099#> <#23100#>z)))))<#23100#> 
Explain why each expression is legal or illegal. external Solution<#64560#><#64560#>