Intermezzo 4: Defining Functions on the Fly

Many uses of abstract functions require the definition of auxiliary functions. Consider <#66036#><#32131#>filter1<#32131#><#66036#>, which consumes a filtering function, a list, and a filtering item. In the previous section alone, we encountered three uses of <#66037#><#32132#>filter1<#32132#><#66037#> with three different auxiliary functions: <#66038#><#32133#>squared?<#32133#><#66038#>, <#66039#>#tex2html_wrap_inline73574#<#66039#>, and <#66040#><#32135#>eq-ir?<#32135#><#66040#>. Since these auxiliary functions are only used as arguments to <#66041#><#32136#>filter1<#32136#><#66041#>, we should employ the program organization guidelines from the preceding intermezzo~(#secintlocal#32137>). That is, we should use a <#66042#><#32138#>local<#32138#>-expression<#66042#> to indicate that the application of <#66043#><#32139#>filter1<#32139#><#66043#> and the auxiliary function definition belong together. Here is one possible arrangement for the <#66044#><#32140#>filter1<#32140#><#66044#><#66045#><#32141#>-eq-ir?<#32141#><#66045#> combination:
<#71440#>;; <#66046#><#32146#>find<#32146#> <#32147#>:<#32147#> <#32148#>list-of-IRs<#32148#> <#32149#>symbol<#32149#> <#32150#><#32150#><#32151#>-;SPMgt;<#32151#><#32152#><#32152#> <#32153#>boolean<#32153#><#66046#><#71440#>
<#32154#>(d<#32154#><#32155#>efine<#32155#> <#32156#>(find<#32156#> <#32157#>aloir<#32157#> <#32158#>t)<#32158#> 
  <#32159#>(l<#32159#><#32160#>ocal<#32160#> <#32161#>((d<#32161#><#32162#>efine<#32162#> <#32163#>(eq-ir?<#32163#> <#32164#>ir<#32164#> <#32165#>p)<#32165#> 
            <#32166#>(symbol=?<#32166#> <#32167#>(ir-name<#32167#> <#32168#>ir)<#32168#> <#32169#>p)))<#32169#> 
    <#32170#>(filter1<#32170#> <#32171#>eq-ir?<#32171#> <#32172#>aloir<#32172#> <#32173#>t)))<#32173#> 
An alternative arrangement places the <#66047#><#32177#>local<#32177#>-expression<#66047#> where the function is needed:
<#71441#>;; <#66048#><#32182#>find<#32182#> <#32183#>:<#32183#> <#32184#>list-of-IRs<#32184#> <#32185#>symbol<#32185#> <#32186#><#32186#><#32187#>-;SPMgt;<#32187#><#32188#><#32188#> <#32189#>boolean<#32189#><#66048#><#71441#>
<#32190#>(d<#32190#><#32191#>efine<#32191#> <#32192#>(find<#32192#> <#32193#>aloir<#32193#> <#32194#>t)<#32194#> 
  <#32195#>(filter1<#32195#> <#32196#>(l<#32196#><#32197#>ocal<#32197#> <#32198#>((d<#32198#><#32199#>efine<#32199#> <#32200#>(eq-ir?<#32200#> <#32201#>ir<#32201#> <#32202#>p)<#32202#> 
                     <#32203#>(symbol=?<#32203#> <#32204#>(ir-name<#32204#> <#32205#>ir)<#32205#> <#32206#>p)))<#32206#> 
             <#32207#>eq-ir?<#32207#><#32208#>)<#32208#> 
           <#32209#>aloir<#32209#> <#32210#>t))<#32210#> 
This alternative is feasible because the names of functions---like <#66049#><#32214#>eq-ir?<#32214#><#66049#>---are now legitimate expressions and can play the role of <#66050#><#32215#>local<#32215#><#66050#>'s body. Thus, the <#66051#><#32216#>local<#32216#>-expression<#66051#> introduces a function definition and returns the function as its result. Because good programmers use abstract functions and organize their programs in a tidy manner, it is not surprising that Scheme provides a short-hand for this particular, frequent use of <#66052#><#32217#>local<#32217#><#66052#>. The short-hand is called a <#66053#><#32218#>lambda<#32218#>-expression<#66053#> and greatly facilitates the introduction of functions like <#66054#><#32219#>eq-ir?<#32219#><#66054#>, <#66055#><#32220#>squared?<#32220#><#66055#>, or <#66056#>#tex2html_wrap_inline73576#<#66056#>. The following two subsections introduce the syntax and semantics of <#66057#><#32222#>lambda<#32222#>-expression<#66057#>s. The last subsection discusses its pragmatics.