Scope and Semantics of lambda

As discussed in the introduction, a <#66075#><#32270#>lambda<#32270#>-expression<#66075#> is just a short-hand for a <#66076#><#32271#>local<#32271#>-expression<#66076#>. In general, we can think of
<#32276#>(lambda<#32276#> <#32277#>(x-1<#32277#> <#32278#>...<#32278#>  <#32279#>x-n)<#32279#> <#32280#>exp)<#32280#>
as
<#32288#>(l<#32288#><#32289#>ocal<#32289#> <#32290#>((define<#32290#> <#32291#>(a-new-name<#32291#> <#32292#>x-1<#32292#> <#32293#>...<#32293#> <#32294#>x-n)<#32294#> <#32295#>exp))<#32295#>
  <#32296#>a-new-name)<#32296#> 
The name of the function, <#66077#><#32300#>a-new-name<#32300#><#66077#> may not occur in <#66078#><#32301#>exp<#32301#><#66078#>. The short-hand explanation suggests that
<#32306#>(lambda<#32306#> <#32307#>(x-1<#32307#> <#32308#>...<#32308#>  <#32309#>x-n)<#32309#> <#32310#>exp)<#32310#>
introduces <#66079#><#32314#>x-1<#32314#><#66079#> ...<#66080#><#32315#>x-n<#32315#><#66080#> as binding occurrences and that the scope of parameters is <#66081#><#32316#>exp<#32316#><#66081#>. Of course, if <#66082#><#32317#>exp<#32317#><#66082#> contains further binding constructs (say a nested <#66083#><#32318#>local<#32318#>-expression<#66083#>), then the scope of the variables may have a hole. Similarly, the explanation implies what basic facts govern the evaluation of <#66084#><#32319#>lambda<#32319#>-expression<#66084#>s:
  1. A <#66085#><#32321#>lambda<#32321#>-expression<#66085#> is a value because functions are values.
  2. The application of <#66086#><#32322#>lambda<#32322#>-expression<#66086#>s to values proceeds according to our usual laws of function application, assuming we expand the short-hand first.
Here is a sample use of <#66087#><#32324#>lambda<#32324#><#66087#>:
  <#32329#>(filter1<#32329#> <#32330#>(lambda<#32330#> <#32331#>(ir<#32331#> <#32332#>p)<#32332#> <#32333#>(;SPMlt;<#32333#> <#32334#>(ir-price<#32334#> <#32335#>ir)<#32335#> <#32336#>p))<#32336#>
           <#32337#>(list<#32337#> <#32338#>(make-ir<#32338#> <#32339#>'<#32339#><#32340#>doll<#32340#> <#32341#>10))<#32341#> 
           <#32342#>8)<#32342#> 
The application of <#66088#><#32346#>filter1<#32346#><#66088#> consumes the <#66089#><#32347#>lambda<#32347#>-expression<#66089#>, a (short) list of inventory records, and a threshold. Given our suggestion, the evaluation can be understood by an expansion of the <#66090#><#32348#>lambda<#32348#>-expression<#66090#> into a corresponding <#66091#><#32349#>local<#32349#>-expression<#66091#>:
<#32354#>...<#32354#>
<#32355#>=<#32355#> <#32356#>(filter1<#32356#> <#32357#>(local<#32357#> <#32358#>((define<#32358#> <#32359#>(<#32359#>#tex2html_wrap_inline73580# <#32361#>ir<#32361#> <#32362#>p)<#32362#> <#32363#>(;SPMlt;<#32363#> <#32364#>(ir-price<#32364#> <#32365#>ir)<#32365#> <#32366#>p)))<#32366#> #tex2html_wrap_inline73582#<#32368#>)<#32368#> 
           <#32369#>(list<#32369#> <#32370#>(make-ir<#32370#> <#32371#>'<#32371#><#32372#>doll<#32372#> <#32373#>10))<#32373#> 
           <#32374#>8)<#32374#> 
<#32375#>=<#32375#> <#32376#>(filter1<#32376#> #tex2html_wrap_inline73584# 
           <#32378#>(list<#32378#> <#32379#>(make-ir<#32379#> <#32380#>'<#32380#><#32381#>doll<#32381#> <#32382#>10))<#32382#> 
           <#32383#>8)<#32383#> 
For the last step, the local definition of <#66092#>#tex2html_wrap_inline73586#<#66092#> is lifted and added to the top-level definitions. From here, the evaluation proceeds as in section~#secddsimilarities#32388>. While it is natural to think of <#66093#><#32389#>lambda<#32389#><#66093#> as a short-hand, the last two points also suggest a way of understanding <#66094#><#32390#>lambda<#32390#>-expression<#66094#>s directly. In particular, we can adapt the law of application to <#66095#><#32391#>lambda<#32391#>-expression<#66095#>s:
  <#32398#>(<#32398#><#32399#>(lambda<#32399#> <#32400#>(x-1<#32400#> <#32401#>...<#32401#> <#32402#>x-n)<#32402#> <#32403#>exp)<#32403#>   
   <#32404#>val-1<#32404#> <#32405#>..<#32405#><#32406#>.<#32406#> <#32407#>val-n)<#32407#>  
   
~
  <#32413#>=<#32413#> <#32414#>ex<#32414#><#32415#>p<#32415#>
    <#72246#>#tex2html_wrap_inline73590#<#72246#> 
    <#72247#>#tex2html_wrap_inline73592#<#72247#> 
That is, the application of a <#66099#><#32425#>lambda<#32425#>-expression<#66099#> proceeds just like that of an ordinary function. We replace the parameters of the function with the actual argument values and compute the value of the function body. Let us reconsider the above example in this light:
  <#32430#>(filter1<#32430#> <#32431#>(lambda<#32431#> <#32432#>(ir<#32432#> <#32433#>p)<#32433#> <#32434#>(;SPMlt;<#32434#> <#32435#>(ir-price<#32435#> <#32436#>ir)<#32436#> <#32437#>p))<#32437#>
           <#32438#>(list<#32438#> <#32439#>(make-ir<#32439#> <#32440#>'<#32440#><#32441#>doll<#32441#> <#32442#>10))<#32442#> 
           <#32443#>8)<#32443#> 
<#71448#>;As usually, this application is replaced by the body of <#66100#><#32444#>filter1<#32444#><#66100#><#71448#> 
<#32445#>;with all parameters replaced by their values. This step places a<#32445#> 
<#71449#>;<#66101#><#32446#>lambda<#32446#>-expression<#66101#> into the function position of an application:<#71449#> 
<#32447#>=<#32447#> <#32448#>(c<#32448#><#32449#>ond<#32449#> 
    <#32450#>[<#32450#><#32451#>(<#32451#><#32452#>(lambda<#32452#> <#32453#>(ir<#32453#> <#32454#>p)<#32454#> <#32455#>(;SPMlt;<#32455#> <#32456#>(ir-price<#32456#> <#32457#>ir)<#32457#> <#32458#>p))<#32458#> 
      <#32459#>(make-ir<#32459#> <#32460#>'<#32460#><#32461#>doll<#32461#> <#32462#>10)<#32462#> <#32463#>8)<#32463#> 
     <#32464#>(c<#32464#><#32465#>ons<#32465#> <#32466#>(first<#32466#> <#32467#>(list<#32467#> <#32468#>(make-ir<#32468#> <#32469#>'<#32469#><#32470#>doll<#32470#> <#32471#>10)))<#32471#> 
       <#32472#>(filter1<#32472#> <#32473#>(lambda<#32473#> <#32474#>(ir<#32474#> <#32475#>p)<#32475#> <#32476#>(;SPMlt;<#32476#> <#32477#>(ir-price<#32477#> <#32478#>ir)<#32478#> <#32479#>p))<#32479#> 
                <#32480#>(rest<#32480#> <#32481#>(list<#32481#> <#32482#>(make-ir<#32482#> <#32483#>'<#32483#><#32484#>doll<#32484#> <#32485#>10)))<#32485#> 
                <#32486#>8))]<#32486#> 
    <#32487#>[<#32487#><#32488#>e<#32488#><#32489#>lse<#32489#> 
      <#32490#>(filter1<#32490#> <#32491#>(lambda<#32491#> <#32492#>(ir<#32492#> <#32493#>p)<#32493#> <#32494#>(;SPMlt;<#32494#> <#32495#>(ir-price<#32495#> <#32496#>ir)<#32496#> <#32497#>p))<#32497#> 
               <#32498#>(rest<#32498#> <#32499#>(list<#32499#> <#32500#>(make-ir<#32500#> <#32501#>'<#32501#><#32502#>doll<#32502#> <#32503#>10)))<#32503#> 
               <#32504#>8)]<#32504#><#32505#>)<#32505#> 
<#32506#>=<#32506#> <#32507#>(c<#32507#><#32508#>ond<#32508#> 
    <#32509#>[<#32509#><#32510#>(;SPMlt;<#32510#> <#32511#>(ir-price<#32511#> <#32512#>(make-ir<#32512#> <#32513#>'<#32513#><#32514#>doll<#32514#> <#32515#>10))<#32515#> <#32516#>8)<#32516#> 
     <#32517#>(c<#32517#><#32518#>ons<#32518#> <#32519#>(first<#32519#> <#32520#>(list<#32520#> <#32521#>(make-ir<#32521#> <#32522#>'<#32522#><#32523#>doll<#32523#> <#32524#>10)))<#32524#> 
       <#32525#>(filter1<#32525#> <#32526#>(lambda<#32526#> <#32527#>(ir<#32527#> <#32528#>p)<#32528#> <#32529#>(;SPMlt;<#32529#> <#32530#>(ir-price<#32530#> <#32531#>ir)<#32531#> <#32532#>p))<#32532#> 
                <#32533#>(rest<#32533#> <#32534#>(list<#32534#> <#32535#>(make-ir<#32535#> <#32536#>'<#32536#><#32537#>doll<#32537#> <#32538#>10)))<#32538#> 
                <#32539#>8))]<#32539#> 
    <#32540#>[<#32540#><#32541#>e<#32541#><#32542#>lse<#32542#> 
      <#32543#>(filter1<#32543#> <#32544#>(lambda<#32544#> <#32545#>(ir<#32545#> <#32546#>p)<#32546#> <#32547#>(;SPMlt;<#32547#> <#32548#>(ir-price<#32548#> <#32549#>ir)<#32549#> <#32550#>p))<#32550#> 
               <#32551#>(rest<#32551#> <#32552#>(list<#32552#> <#32553#>(make-ir<#32553#> <#32554#>'<#32554#><#32555#>doll<#32555#> <#32556#>10)))<#32556#> 
               <#32557#>8)]<#32557#><#32558#>)<#32558#> 
<#32559#>=<#32559#> <#32560#>...<#32560#> 
From here, the evaluation proceeds as usual. Still, even this short hand-evaluation shows that, while using <#66102#><#32564#>lambda<#32564#>-expression<#66102#>s in programs is convenient, replacing it with a named function (often) simplifies calculations.
<#32567#>Exercise 24.2.1<#32567#> Decide which of the following phrases are legal <#66103#><#32569#>lambda<#32569#>-expression<#66103#>s:
  1. <#66104#><#32571#>(lambda<#32571#>\ <#32572#>(x<#32572#>\ <#32573#>y)<#32573#>\ <#32574#>(x<#32574#>\ <#32575#>y<#32575#>\ <#32576#>y))<#32576#><#66104#>
  2. <#66105#><#32577#>(lambda<#32577#>\ <#32578#>()<#32578#>\ <#32579#>10)<#32579#><#66105#>
  3. <#66106#><#32580#>(lambda<#32580#>\ <#32581#>(x)<#32581#>\ <#32582#>x)<#32582#><#66106#>
  4. <#66107#><#32583#>(lambda<#32583#>\ <#32584#>(x<#32584#>\ <#32585#>y)<#32585#>\ <#32586#>x)<#32586#><#66107#>
  5. <#66108#><#32587#>(lambda<#32587#>\ <#32588#>x<#32588#>\ <#32589#>10)<#32589#><#66108#>
Explain why they are legal or illegal. external Solution<#66109#><#66109#> <#32596#>Exercise 24.2.2<#32596#> Draw arrows from the underlined occurrences of <#66110#><#32598#>x<#32598#><#66110#> to their binding occurrences in each of the following three <#66111#><#32599#>lambda<#32599#>-expression<#66111#>s:
  1. ~~
    <#32604#>(l<#32604#><#32605#>ambda<#32605#> <#32606#>(x<#32606#> <#32607#>y)<#32607#>
      <#32608#>(+<#32608#> <#66112#>#tex2html_wrap_inline73594#<#66112#> <#32610#>(*<#32610#> <#32611#>x<#32611#> <#32612#>y)))<#32612#> 
    
  2. ~~
    <#32618#>(l<#32618#><#32619#>ambda<#32619#> <#32620#>(x<#32620#> <#32621#>y)<#32621#>
      <#32622#>(+<#32622#> <#66113#>#tex2html_wrap_inline73596#<#66113#> 
         <#32624#>(l<#32624#><#32625#>ocal<#32625#> <#32626#>((define<#32626#> <#32627#>x<#32627#> <#32628#>(*<#32628#> <#32629#>y<#32629#> <#32630#>y)))<#32630#> 
           <#32631#>(+<#32631#> <#32632#>(*<#32632#> <#32633#>3<#32633#> <#66114#>#tex2html_wrap_inline73598#<#66114#><#32635#>)<#32635#> 
              <#32636#>(/<#32636#> <#32637#>1<#32637#> <#32638#>x)))))<#32638#> 
    
  3. ~~
    <#32644#>(l<#32644#><#32645#>ambda<#32645#> <#32646#>(x<#32646#> <#32647#>y)<#32647#>
      <#32648#>(+<#32648#> <#66115#>#tex2html_wrap_inline73600#<#66115#> 
         <#32650#>(<#32650#><#32651#>(l<#32651#><#32652#>ambda<#32652#> <#32653#>(x)<#32653#> 
            <#32654#>(+<#32654#> <#32655#>(*<#32655#> <#32656#>3<#32656#> <#66116#>#tex2html_wrap_inline73602#<#66116#><#32658#>)<#32658#> 
               <#32659#>(/<#32659#> <#32660#>1<#32660#> <#32661#>x)))<#32661#> 
          <#32662#>(*<#32662#> <#32663#>y<#32663#> <#32664#>y))))<#32664#> 
    
Also draw a box for the corresponding scope of each underlined <#66117#><#32668#>x<#32668#><#66117#> and holes in the scope where necessary. external Solution<#66118#><#66118#> <#32674#>Exercise 24.2.3<#32674#> Evaluate the following expressions by hand:
  1. ~~
    <#32680#>(<#32680#><#32681#>(l<#32681#><#32682#>ambda<#32682#> <#32683#>(x<#32683#> <#32684#>y)<#32684#>
       <#32685#>(+<#32685#> <#66119#>#tex2html_wrap_inline73604#<#66119#> <#32687#>(*<#32687#> <#32688#>x<#32688#> <#32689#>y)))<#32689#> 
     <#32690#>1<#32690#> <#32691#>2)<#32691#> 
    
  2. ~~
    <#32697#>(<#32697#><#32698#>(l<#32698#><#32699#>ambda<#32699#> <#32700#>(x<#32700#> <#32701#>y)<#32701#>
       <#32702#>(+<#32702#> <#66120#>#tex2html_wrap_inline73606#<#66120#> 
          <#32704#>(l<#32704#><#32705#>ocal<#32705#> <#32706#>((define<#32706#> <#32707#>x<#32707#> <#32708#>(*<#32708#> <#32709#>y<#32709#> <#32710#>y)))<#32710#> 
            <#32711#>(+<#32711#> <#32712#>(*<#32712#> <#32713#>3<#32713#> <#66121#>#tex2html_wrap_inline73608#<#66121#><#32715#>)<#32715#> 
               <#32716#>(/<#32716#> <#32717#>1<#32717#> <#32718#>x)))))<#32718#> 
     <#32719#>1<#32719#> <#32720#>2)<#32720#> 
    
  3. ~~
    <#32726#>(<#32726#><#32727#>(l<#32727#><#32728#>ambda<#32728#> <#32729#>(x<#32729#> <#32730#>y)<#32730#>
       <#32731#>(+<#32731#> <#66122#>#tex2html_wrap_inline73610#<#66122#> 
          <#32733#>(<#32733#><#32734#>(l<#32734#><#32735#>ambda<#32735#> <#32736#>(x)<#32736#> 
             <#32737#>(+<#32737#> <#32738#>(*<#32738#> <#32739#>3<#32739#> <#66123#>#tex2html_wrap_inline73612#<#66123#><#32741#>)<#32741#> 
                <#32742#>(/<#32742#> <#32743#>1<#32743#> <#32744#>x)))<#32744#> 
           <#32745#>(*<#32745#> <#32746#>y<#32746#> <#32747#>y))))<#32747#> 
     <#32748#>1<#32748#> <#32749#>2)<#32749#> 
    
external Solution<#66124#><#66124#>