Recognizing the Need for Accumulator

Recognizing the need for accumulators is not an easy task. We have seen two reasons, and they are the most prevalent reasons for adding accumulator parameters. In either case, it is critical that we first built a complete function <#40612#>based on a design recipe<#40612#>. Then we study the function and look for one of the following characteristics:
  1. If the function is structurally recursive and if the result of a recursive application is processed by an auxiliary, recursive function, then we should consider the use of an accumulator parameter. Take the function <#67492#><#40614#>reverse<#40614#><#67492#> for an example:
    <#71613#>;; <#67493#><#40619#>reverse<#40619#> <#40620#>:<#40620#> <#40621#>(listof<#40621#> <#40622#>X)<#40622#> <#40623#><#40623#><#40624#>-;SPMgt;<#40624#><#40625#><#40625#> <#40626#>(listof<#40626#> <#40627#>X)<#40627#><#67493#><#71613#>
    <#71614#>;; to construct the reverse of <#67494#><#40628#>alox<#40628#><#67494#><#71614#> 
    <#40629#>;; structural recursion <#40629#> 
    <#40630#>(d<#40630#><#40631#>efine<#40631#> <#40632#>(reverse<#40632#> <#40633#>alox)<#40633#> 
      <#40634#>(c<#40634#><#40635#>ond<#40635#> 
        <#40636#>[<#40636#><#40637#>(empty?<#40637#> <#40638#>alox)<#40638#> <#40639#>empty]<#40639#> 
        <#40640#>[<#40640#><#40641#>else<#40641#> <#40642#>(make-last-item<#40642#> <#40643#>(reverse<#40643#> <#40644#>(rest<#40644#> <#40645#>alox))<#40645#> <#40646#>(first<#40646#> <#40647#>alox))]<#40647#><#40648#>))<#40648#> 
    <#71615#>;; <#67495#><#40649#>make-last-item<#40649#> <#40650#>:<#40650#> <#40651#>X<#40651#> <#40652#>(listof<#40652#> <#40653#>X)<#40653#> <#40654#><#40654#><#40655#>-;SPMgt;<#40655#><#40656#><#40656#> <#40657#>(listof<#40657#> <#40658#>X)<#40658#><#67495#><#71615#> 
    <#71616#>;; to add <#67496#><#40659#>an-x<#40659#><#67496#> to the end of <#67497#><#40660#>alox<#40660#><#67497#><#71616#> 
    <#40661#>;; structural recursion <#40661#> 
    <#40662#>(d<#40662#><#40663#>efine<#40663#> <#40664#>(make-last-item<#40664#> <#40665#>an-x<#40665#> <#40666#>alox)<#40666#> 
      <#40667#>(c<#40667#><#40668#>ond<#40668#> 
        <#40669#>[<#40669#><#40670#>(empty?<#40670#> <#40671#>alox)<#40671#> <#40672#>(list<#40672#> <#40673#>an-x)]<#40673#> 
        <#40674#>[<#40674#><#40675#>else<#40675#> <#40676#>(cons<#40676#> <#40677#>(first<#40677#> <#40678#>alox)<#40678#> <#40679#>(make-last-item<#40679#> <#40680#>an-x<#40680#> <#40681#>(rest<#40681#> <#40682#>alox)))]<#40682#><#40683#>))<#40683#> 
    
    The result of the recursive application produces the reverse of the rest of the list. It is processed by <#67498#><#40687#>make-last-item<#40687#><#67498#>, which adds the first item to the reverse of the rest and thus creates the reverse of the entire list. This second, auxiliary function is also recursive. We have thus identified a potential candidate. It is now time to study some hand-evaluations, as we did in section~#seclossstructural#40688>, to see whether an accumulator helps.
  2. If we are dealing with a function based on generative recursion, we are faced with a much more difficult task. Our goal must be to understand whether the algorithm can fail to produce a result for inputs for which we expect a result. If so, adding a parameter that accumulates knowledge may help. Because these situations are complex, we postpone the discussion of an example until section~#secmoreaccumc#40689>.
These two situations are by no means the only ones; they are just the most common ones. To sharpen our perception, we will discuss an additional array of possibilities in the following section.