file no9 file no9

Pragmatics of local, Part 2

Suppose we need a function that produces the last occurrence of some item in a list. To be precise, assume we have lists of records on rock stars. For simplicity, each star is represented as a pair of values:
<#24086#>(define-struct<#24086#> <#24087#>star<#24087#> <#24088#>(name<#24088#> <#24089#>instrument))<#24089#>
A <#64644#><#24094#>star<#24094#><#64644#> (record) is a structure:

<#71186#><#64645#><#24095#>(make-star<#24095#>\ <#24096#>s<#24096#>\ <#24097#>t)<#24097#><#64645#><#71186#> where <#64646#><#24098#>s<#24098#><#64646#> and <#64647#><#24099#>t<#24099#><#64647#> are symbols.

Here is an example:
<#24105#>(d<#24105#><#24106#>efine<#24106#> <#24107#>alos<#24107#> 
  <#24108#>(list<#24108#> <#24109#>(make-star<#24109#> <#24110#>'<#24110#><#24111#>Chris<#24111#> <#24112#>'<#24112#><#24113#>saxophone)<#24113#> 
        <#24114#>(make-star<#24114#> <#24115#>'<#24115#><#24116#>Robby<#24116#> <#24117#>'<#24117#><#24118#>trumpet)<#24118#> 
        <#24119#>(make-star<#24119#> <#24120#>'<#24120#><#24121#>Matt<#24121#> <#24122#>'<#24122#><#24123#>violin)<#24123#> 
        <#24124#>(make-star<#24124#> <#24125#>'<#24125#><#24126#>Wen<#24126#> <#24127#>'<#24127#><#24128#>guitar)<#24128#> 
        <#24129#>(make-star<#24129#> <#24130#>'<#24130#><#24131#>Matt<#24131#> <#24132#>'<#24132#><#24133#>radio)))<#24133#> 
This list contains two occurrences of <#64648#><#24137#>'<#24137#><#24138#>Matt<#24138#><#64648#>. So, if we wanted to determine the instrument that goes with the last occurrence of <#64649#><#24139#>'<#24139#><#24140#>Matt<#24140#><#64649#>, we would want <#64650#><#24141#>'<#24141#><#24142#>radio<#24142#><#64650#>. For <#64651#><#24143#>'<#24143#><#24144#>Sean<#24144#><#64651#>, on the other hand, our function would produce <#64652#><#24145#>'<#24145#><#24146#>guitar<#24146#><#64652#>. Of course, looking for the instrument of <#64653#><#24147#>'<#24147#><#24148#>Kate<#24148#><#64653#> should yield <#64654#><#24149#>false<#24149#><#64654#> to indicate that there is no record for <#64655#><#24150#>'<#24150#><#24151#>Kate<#24151#><#64655#>. Let's write down a contract, a purpose statement, and a header:
<#71187#>;; <#64656#><#24156#>last-occurrence<#24156#> <#24157#>:<#24157#> <#24158#>symbol<#24158#> <#24159#>(listof<#24159#> <#24160#>star)<#24160#> <#24161#><#24161#><#24162#>-;SPMgt;<#24162#><#24163#><#24163#> <#24164#>star<#24164#> <#24165#>or<#24165#> <#24166#>false<#24166#><#64656#><#71187#>
<#71188#>;; to find the last star record in <#64657#><#24167#>alostars<#24167#><#64657#> that contains <#64658#><#24168#>s<#24168#><#64658#> in <#64659#><#24169#>name<#24169#><#64659#> field<#71188#> 
<#24170#>(define<#24170#> <#24171#>(last-occurrence<#24171#> <#24172#>s<#24172#> <#24173#>alostars)<#24173#> <#24174#>...)<#24174#> 
The contract is unusual because it mentions two classes of data to the right of the arrow: <#64660#><#24178#>star<#24178#><#64660#> and <#64661#><#24179#>false<#24179#><#64661#>. Although we haven't seen this kind of contract before, its meaning is obvious. The function may produce a <#64662#><#24180#>star<#24180#><#64662#> or <#64663#><#24181#>false<#24181#><#64663#>. We have already developed some examples, so we can move directly to the template stage of our design recipe:
<#24186#>(d<#24186#><#24187#>efine<#24187#> <#24188#>(last-occurrence<#24188#> <#24189#>s<#24189#> <#24190#>alostars)<#24190#>
  <#24191#>(c<#24191#><#24192#>ond<#24192#> 
    <#24193#>[<#24193#><#24194#>(empty?<#24194#> <#24195#>alostars)<#24195#> <#24196#>...]<#24196#> 
    <#24197#>[<#24197#><#24198#>else<#24198#> <#24199#>...<#24199#> <#24200#>(first<#24200#> <#24201#>alostars)<#24201#> <#24202#>...<#24202#> <#24203#>(last-occurrence<#24203#> <#24204#>s<#24204#> <#24205#>(rest<#24205#> <#24206#>alostars))<#24206#> <#24207#>...]<#24207#><#24208#>))<#24208#>