Functions that Produce Functions

While the idea of producing a function may seem strange at first, it is extremely useful. Before we can discuss the usefulness of the idea, though, we must explore how a function can produce a function. Here are three examples:
<#29654#>(define<#29654#> <#29655#>(f<#29655#> <#29656#>x)<#29656#> <#29657#>first)<#29657#>
<#29658#>(define<#29658#> <#29659#>(g<#29659#> <#29660#>x)<#29660#> <#29661#>f)<#29661#> 
<#29662#>(d<#29662#><#29663#>efine<#29663#> <#29664#>(h<#29664#> <#29665#>x)<#29665#> 
  <#29666#>(c<#29666#><#29667#>ond<#29667#> 
    <#29668#>((empty?<#29668#> <#29669#>x)<#29669#> <#29670#>f)<#29670#> 
    <#29671#>((cons?<#29671#> <#29672#>x)<#29672#> <#29673#>g)))<#29673#> 
The body of <#65574#><#29677#>f<#29677#><#65574#> is <#65575#><#29678#>first<#29678#><#65575#>, a primitive operation, so applying <#65576#><#29679#>f<#29679#><#65576#> to any argument always evaluates to <#65577#><#29680#>first<#29680#><#65577#>. Similarly, the body of <#65578#><#29681#>g<#29681#><#65578#> is <#65579#><#29682#>f<#29682#><#65579#>, so applying <#65580#><#29683#>g<#29683#><#65580#> to any argument always evaluates to <#65581#><#29684#>f<#29684#><#65581#>. Finally, depending on what kind of list we supply as an argument to <#65582#><#29685#>h<#29685#><#65582#>, it produces <#65583#><#29686#>f<#29686#><#65583#> or <#65584#><#29687#>g<#29687#><#65584#>. None of these examples are useful but they illustrate the basic idea. In the first two cases, the body of the function definition <#29688#> is<#29688#> a function. In the last case, it <#29689#> evaluates to<#29689#> a function. The examples are useless, however, because the results do not contain or refer to the argument. For a function <#65585#><#29690#>f<#29690#><#65585#> to produce a function that contains one of <#65586#><#29691#>f<#29691#><#65586#>'s arguments, <#65587#><#29692#>f<#29692#><#65587#> must define a function and return it as the result. In short, <#65588#><#29693#>f<#29693#><#65588#>'s body must be a <#65589#><#29694#>local<#29694#>-expression<#65589#>. Recall that <#65590#><#29695#>local<#29695#>-expression<#65590#>s group definitions and ask DrScheme to evaluate a single expression in the context of these definitions. They can occur wherever an expression may occur, which means the following definition is legal:
<#29700#>(d<#29700#><#29701#>efine<#29701#> <#29702#>(add<#29702#> <#29703#>x)<#29703#>
  <#29704#>(l<#29704#><#29705#>ocal<#29705#> <#29706#>((define<#29706#> <#29707#>(x-adder<#29707#> <#29708#>y)<#29708#> <#29709#>(+<#29709#> <#29710#>x<#29710#> <#29711#>y)))<#29711#> 
    <#29712#>x-adder))<#29712#> 
The function <#65591#><#29716#>add<#29716#><#65591#> consumes a number; after all, <#65592#><#29717#>x<#29717#><#65592#> is added to <#65593#><#29718#>y<#29718#><#65593#>. It then defines the function <#65594#><#29719#>x-adder<#29719#><#65594#> with a <#65595#><#29720#>local<#29720#>-expression<#65595#>. The body of the <#65596#><#29721#>local<#29721#>-expression<#65596#> is <#65597#><#29722#>x-adder<#29722#><#65597#>, which means the result of <#65598#><#29723#>add<#29723#><#65598#> is <#65599#><#29724#>x-adder<#29724#><#65599#>. To understand <#65600#><#29725#>add<#29725#><#65600#> better, let us look at how an application of <#65601#><#29726#>add<#29726#><#65601#> to some number evaluates:
  <#29731#>(define<#29731#> <#29732#>f<#29732#> <#29733#>(add<#29733#> <#29734#>5))<#29734#>
<#29735#>=<#29735#> <#29736#>(define<#29736#> <#29737#>f<#29737#> <#29738#>(l<#29738#><#29739#>ocal<#29739#> <#29740#>((define<#29740#> <#29741#>(x-adder<#29741#> <#29742#>y)<#29742#> <#29743#>(+<#29743#> <#29744#>5<#29744#> <#29745#>y)))<#29745#> 
              <#29746#>x-adder))<#29746#> 
<#29747#>=<#29747#> <#29748#>(define<#29748#> <#29749#>f<#29749#> <#29750#>(l<#29750#><#29751#>ocal<#29751#> <#29752#>((define<#29752#> <#29753#>(x-adder5<#29753#> <#29754#>y)<#29754#> <#29755#>(+<#29755#> <#29756#>5<#29756#> <#29757#>y)))<#29757#> 
              <#29758#>x-adder5))<#29758#> 
<#29759#>=<#29759#> <#29760#>(define<#29760#> <#29761#>(x-adder5<#29761#> <#29762#>y)<#29762#> <#29763#>(+<#29763#> <#29764#>5<#29764#> <#29765#>y))<#29765#> 
  <#29766#>(define<#29766#> <#29767#>f<#29767#> <#29768#>x-adder5)<#29768#> 
That is, the last step adds the function <#65602#><#29772#>x-adder5<#29772#><#65602#> to the collection of our definitions; the evaluation continues with the body of the <#65603#><#29773#>local<#29773#>-expression<#65603#>, <#65604#><#29774#>x-adder5<#29774#><#65604#>, which is the name of a function and thus a value. Now <#65605#><#29775#>f<#29775#><#65605#> is defined and we can use it:
  <#29780#>(f<#29780#> <#29781#>10)<#29781#>
<#29782#>=<#29782#> <#29783#>(x-adder5<#29783#> <#29784#>10)<#29784#> 
<#29785#>=<#29785#> <#29786#>(+<#29786#> <#29787#>5<#29787#> <#29788#>10)<#29788#> 
<#29789#>=<#29789#> <#29790#>15<#29790#> 
That is, <#65606#><#29794#>f<#29794#><#65606#> stands for <#65607#><#29795#>x-adder5<#29795#><#65607#>, a function, which adds <#65608#><#29796#>5<#29796#><#65608#> to its argument. Using this example, we can write <#65609#><#29797#>add<#29797#><#65609#>'s contract and a purpose statement:
<#71377#>;; <#65610#><#29802#>add<#29802#> <#29803#>:<#29803#> <#29804#>number<#29804#> <#29805#><#29805#><#29806#>-;SPMgt;<#29806#><#29807#><#29807#> <#29808#>(number<#29808#> <#29809#><#29809#><#29810#>-;SPMgt;<#29810#><#29811#><#29811#> <#29812#>number)<#29812#><#65610#><#71377#>
<#71378#>;; to create a function that adds <#65611#><#29813#>x<#29813#><#65611#> to its input<#71378#> 
<#29814#>(d<#29814#><#29815#>efine<#29815#> <#29816#>(add<#29816#> <#29817#>x)<#29817#> 
  <#29818#>(l<#29818#><#29819#>ocal<#29819#> <#29820#>((define<#29820#> <#29821#>(x-adder<#29821#> <#29822#>y)<#29822#> <#29823#>(+<#29823#> <#29824#>x<#29824#> <#29825#>y)))<#29825#> 
    <#29826#>x-adder))<#29826#> 
The most interesting property of <#65612#><#29830#>add<#29830#><#65612#> is that its result ``remembers'' the value of <#65613#><#29831#>x<#29831#><#65613#>. For example, every time we use <#65614#><#29832#>f<#29832#><#65614#>, it uses <#65615#><#29833#>5<#29833#><#65615#>, the value of <#65616#><#29834#>x<#29834#><#65616#>, when <#65617#><#29835#>add<#29835#><#65617#> was used to define <#65618#><#29836#>f<#29836#><#65618#>. This form of ``memory'' is the key to our simple recipe for defining abstract functions, which we discuss in the next section.