Finger Exercises with Abstract List Functions

Scheme provides a number of abstract functions for processing lists. Figure~#figschemeholist#28958> collects the specification of the most important ones. Using these functions greatly simplifies many programming tasks and helps readers understand programs quickly. The following exercises provide an opportunity to get acquainted with these functions.
<#71350#>;; <#65465#><#28963#>build-list<#28963#> <#28964#>:<#28964#> <#28965#>N<#28965#> <#28966#>(<#28966#><#28967#>N<#28967#> <#28968#><#28968#><#28969#>-;SPMgt;<#28969#><#28970#><#28970#> <#28971#>X)<#28971#> <#28972#><#28972#><#28973#>-;SPMgt;<#28973#><#28974#><#28974#> <#28975#>(listof<#28975#> <#28976#>X)<#28976#><#65465#><#71350#>
<#71351#>;; to construct <#65466#><#28977#>(list<#28977#> <#28978#>(f<#28978#> <#28979#>0)<#28979#> <#28980#>...<#28980#> <#28981#>(f<#28981#> <#28982#>(-<#28982#> <#28983#>n<#28983#> <#28984#>1)))<#28984#><#65466#><#71351#> 
<#28985#>(define<#28985#> <#28986#>(build-list<#28986#> <#28987#>n<#28987#> <#28988#>f)<#28988#> <#28989#>...)<#28989#> 
<#71352#>;; <#65467#><#28990#>filter<#28990#> <#28991#>:<#28991#> <#28992#>(X<#28992#> <#28993#><#28993#><#28994#>-;SPMgt;<#28994#><#28995#><#28995#> <#28996#>boolean)<#28996#> <#28997#>(listof<#28997#> <#28998#>X)<#28998#> <#28999#><#28999#><#29000#>-;SPMgt;<#29000#><#29001#><#29001#> <#29002#>(listof<#29002#> <#29003#>X)<#29003#><#65467#><#71352#> 
<#71353#>;; to construct a list from all those items on <#65468#><#29004#>alox<#29004#><#65468#> for which <#65469#><#29005#>p<#29005#><#65469#> holds <#71353#> 
<#29006#>(define<#29006#> <#29007#>(filter<#29007#> <#29008#>p<#29008#> <#29009#>alox)<#29009#> <#29010#>...)<#29010#> 
<#71354#>;; <#65470#><#29011#>sort<#29011#> <#29012#>:<#29012#> <#29013#>(X<#29013#> <#29014#>X<#29014#> <#29015#><#29015#><#29016#>-;SPMgt;<#29016#><#29017#><#29017#> <#29018#>boolean)<#29018#> <#29019#>(listof<#29019#> <#29020#>X)<#29020#> <#29021#><#29021#><#29022#>-;SPMgt;<#29022#><#29023#><#29023#> <#29024#>(listof<#29024#> <#29025#>X)<#29025#><#65470#><#71354#> 
<#71355#>;; to construct a list from all items on <#65471#><#29026#>alox<#29026#><#65471#> in an order according to <#65472#><#29027#>cmp<#29027#><#65472#><#71355#> 
<#29028#>(define<#29028#> <#29029#>(sort<#29029#> <#29030#>cmp<#29030#> <#29031#>alox)<#29031#> <#29032#>...)<#29032#> 
<#71356#>;; <#65473#><#29033#>map<#29033#> <#29034#>:<#29034#> <#29035#>(X<#29035#> <#29036#><#29036#><#29037#>-;SPMgt;<#29037#><#29038#><#29038#> <#29039#>Y)<#29039#> <#29040#>(listof<#29040#> <#29041#>X)<#29041#> <#29042#><#29042#><#29043#>-;SPMgt;<#29043#><#29044#><#29044#> <#29045#>(listof<#29045#> <#29046#>Y)<#29046#><#65473#><#71356#> 
<#71357#>;; to construct a list by applying <#65474#><#29047#>f<#29047#><#65474#> to each item on <#65475#><#29048#>alox<#29048#><#65475#><#71357#> 
<#71358#>;; that is, <#65476#><#29049#>(map<#29049#> <#29050#>f<#29050#> <#29051#>(list<#29051#> <#29052#>x-1<#29052#> <#29053#>...<#29053#> <#29054#>x-n))<#29054#><#65476#> = <#65477#><#29055#>(list<#29055#> <#29056#>(f<#29056#> <#29057#>x-1)<#29057#> <#29058#>...<#29058#> <#29059#>(f<#29059#> <#29060#>x-n))<#29060#><#65477#><#71358#> 
<#29061#>(define<#29061#> <#29062#>(map<#29062#> <#29063#>f<#29063#> <#29064#>alox)<#29064#> <#29065#>...)<#29065#> 
<#71359#>;; <#65478#><#29066#>andmap<#29066#> <#29067#>:<#29067#> <#29068#>(X<#29068#> <#29069#><#29069#><#29070#>-;SPMgt;<#29070#><#29071#><#29071#> <#29072#>boolean)<#29072#> <#29073#>(listof<#29073#> <#29074#>X)<#29074#> <#29075#><#29075#><#29076#>-;SPMgt;<#29076#><#29077#><#29077#> <#29078#>boolean<#29078#><#65478#><#71359#> 
<#71360#>;; to determine whether <#65479#><#29079#>p<#29079#><#65479#> holds for every item on <#65480#><#29080#>alox<#29080#><#65480#><#71360#> 
<#71361#>;; that is, <#65481#><#29081#>(andmap<#29081#> <#29082#>p<#29082#> <#29083#>(list<#29083#> <#29084#>x-1<#29084#> <#29085#>...<#29085#> <#29086#>x-n))<#29086#><#65481#> = <#65482#><#29087#>(and<#29087#> <#29088#>(p<#29088#> <#29089#>x-1)<#29089#> <#29090#>(and<#29090#> <#29091#>...<#29091#> <#29092#>(p<#29092#> <#29093#>x-n)))<#29093#><#65482#><#71361#> 
<#29094#>(define<#29094#> <#29095#>(andmap<#29095#> <#29096#>p<#29096#> <#29097#>alox)<#29097#> <#29098#>...)<#29098#> 
<#71362#>;; <#65483#><#29099#>ormap<#29099#> <#29100#>:<#29100#> <#29101#>(X<#29101#> <#29102#><#29102#><#29103#>-;SPMgt;<#29103#><#29104#><#29104#> <#29105#>boolean)<#29105#> <#29106#>(listof<#29106#> <#29107#>X)<#29107#> <#29108#><#29108#><#29109#>-;SPMgt;<#29109#><#29110#><#29110#> <#29111#>boolean<#29111#><#65483#><#71362#> 
<#71363#>;; to determine whether <#65484#><#29112#>p<#29112#><#65484#> holds for at least one item on <#65485#><#29113#>alox<#29113#><#65485#><#71363#> 
<#71364#>;; that is, <#65486#><#29114#>(ormap<#29114#> <#29115#>p<#29115#> <#29116#>(list<#29116#> <#29117#>x-1<#29117#> <#29118#>...<#29118#> <#29119#>x-n))<#29119#><#65486#> = <#65487#><#29120#>(or<#29120#> <#29121#>(p<#29121#> <#29122#>x-1)<#29122#> <#29123#>(or<#29123#> <#29124#>...<#29124#> <#29125#>(p<#29125#> <#29126#>x-n)))<#29126#><#65487#><#71364#> 
<#29127#>(define<#29127#> <#29128#>(ormap<#29128#> <#29129#>p<#29129#> <#29130#>alox)<#29130#> <#29131#>...)<#29131#> 
<#71365#>;; <#65488#><#29132#>foldr<#29132#> <#29133#>:<#29133#> <#29134#>(X<#29134#> <#29135#>Y<#29135#> <#29136#><#29136#><#29137#>-;SPMgt;<#29137#><#29138#><#29138#> <#29139#>Y)<#29139#> <#29140#>Y<#29140#> <#29141#>(listof<#29141#> <#29142#>X)<#29142#> <#29143#><#29143#><#29144#>-;SPMgt;<#29144#><#29145#><#29145#> <#29146#>Y<#29146#><#65488#><#71365#> 
<#71366#>;; <#65489#><#29147#>(foldr<#29147#> <#29148#>f<#29148#> <#29149#>base<#29149#> <#29150#>(list<#29150#> <#29151#>x-1<#29151#> <#29152#>...<#29152#> <#29153#>x-n))<#29153#> <#29154#>=<#29154#> <#29155#>(f<#29155#> <#29156#>x-1<#29156#> <#29157#>...<#29157#> <#29158#>(f<#29158#> <#29159#>x-n<#29159#> <#29160#>base))<#29160#><#65489#> <#71366#> 
<#29161#>(define<#29161#> <#29162#>(foldr<#29162#> <#29163#>f<#29163#> <#29164#>base<#29164#> <#29165#>alox)<#29165#> <#29166#>...)<#29166#> 
<#71367#>;; <#65490#><#29167#>foldl<#29167#> <#29168#>:<#29168#> <#29169#>(X<#29169#> <#29170#>Y<#29170#> <#29171#><#29171#><#29172#>-;SPMgt;<#29172#><#29173#><#29173#> <#29174#>Y)<#29174#> <#29175#>Y<#29175#> <#29176#>(listof<#29176#> <#29177#>X)<#29177#> <#29178#><#29178#><#29179#>-;SPMgt;<#29179#><#29180#><#29180#> <#29181#>Y<#29181#><#65490#><#71367#> 
<#71368#>;; <#65491#><#29182#>(foldl<#29182#> <#29183#>f<#29183#> <#29184#>base<#29184#> <#29185#>(list<#29185#> <#29186#>x-1<#29186#> <#29187#>...<#29187#> <#29188#>x-n))<#29188#> <#29189#>=<#29189#> <#29190#>(f<#29190#> <#29191#>x-n<#29191#> <#29192#>...<#29192#> <#29193#>(f<#29193#> <#29194#>x-1<#29194#> <#29195#>base))<#29195#><#65491#> <#71368#> 
<#29196#>(define<#29196#> <#29197#>(foldl<#29197#> <#29198#>f<#29198#> <#29199#>base<#29199#> <#29200#>alox)<#29200#> <#29201#>...)<#29201#> 
<#29205#>Figure: Scheme's built-in abstract functions for list-processing<#29205#>

<#29209#>Exercise 21.2.1<#29209#> Use <#65492#><#29211#>build-list<#29211#><#65492#>
  1. to create the lists <#65493#><#29213#>(list<#29213#>\ <#29214#>0<#29214#>\ <#29215#>...<#29215#>\ <#29216#>3)<#29216#><#65493#> and <#65494#><#29217#>(list<#29217#>\ <#29218#>1<#29218#>\ <#29219#>...<#29219#>\ <#29220#>4)<#29220#><#65494#>;
  2. to create the list <#65495#><#29221#>(list<#29221#>\ <#29222#>.1<#29222#>\ <#29223#>.01<#29223#>\ <#29224#>.001<#29224#>\ <#29225#>.0001)<#29225#><#65495#>;
  3. to define <#65496#><#29226#>evens<#29226#><#65496#>, which consumes a natural numbers <#65497#><#29227#>n<#29227#><#65497#> and creates the list of the first <#65498#><#29228#>n<#29228#><#65498#> even numbers;
  4. to define <#65499#><#29229#>tabulate<#29229#><#65499#> from exercise~#exabstabulate#29230>; and
  5. to define <#65500#><#29231#>diagonal<#29231#><#65500#>, which consumes a natural number <#65501#><#29232#>n<#29232#><#65501#> and creates a list of lists of <#65502#><#29233#>0<#29233#><#65502#> and <#65503#><#29234#>1<#29234#><#65503#>.
    Example:
      <#29239#>(diagonal<#29239#> <#29240#>3)<#29240#>
    <#29241#>=<#29241#> <#29242#>(l<#29242#><#29243#>ist<#29243#> 
        <#29244#>(list<#29244#> <#29245#>1<#29245#> <#29246#>0<#29246#> <#29247#>0)<#29247#> 
        <#29248#>(list<#29248#> <#29249#>0<#29249#> <#29250#>1<#29250#> <#29251#>0)<#29251#> 
        <#29252#>(list<#29252#> <#29253#>0<#29253#> <#29254#>0<#29254#> <#29255#>1))<#29255#> 
    
Use local if function definitions require auxiliary functions.~ external Solution<#65504#><#65504#> <#29265#>Exercise 21.2.2<#29265#> Use <#65505#><#29267#>map<#29267#><#65505#> to define the following functions:
  1. <#65506#><#29269#>convertDM<#29269#><#65506#>, which converts a list of US Dollar amounts into a list of Deutsche Mark amounts based on an exchange rate of 1.82 marks for each dollar;
  2. <#65507#><#29270#>convertFC<#29270#><#65507#>, which converts a list of Fahrenheit measurements to a list of Celsius measurements;
  3. <#65508#><#29271#>move-all<#29271#><#65508#>, which consumes a list of <#65509#><#29272#>posn<#29272#><#65509#> structures and translates each by adding <#65510#><#29273#>3<#29273#><#65510#> to the x-component.~ external Solution<#65511#><#65511#>
<#29280#>Exercise 21.2.3<#29280#> Here is the version of <#65512#><#29282#>filter<#29282#><#65512#> that DrScheme provides:
 
<#71369#>;; <#65513#><#29287#>filter<#29287#> <#29288#>:<#29288#> <#29289#>(X<#29289#> <#29290#><#29290#><#29291#>-;SPMgt;<#29291#><#29292#><#29292#> <#29293#>boolean)<#29293#> <#29294#>(listof<#29294#> <#29295#>X)<#29295#> <#29296#><#29296#><#29297#>-;SPMgt;<#29297#><#29298#><#29298#> <#29299#>(listof<#29299#> <#29300#>X)<#29300#><#65513#><#71369#> 
<#71370#>;; to construct a list of <#65514#><#29301#>X<#29301#><#65514#> from all those items on <#65515#><#29302#>alon<#29302#><#65515#><#71370#> 
<#71371#>;; for which <#65516#><#29303#>predicate?<#29303#><#65516#> holds <#71371#> 
<#29304#>(d<#29304#><#29305#>efine<#29305#> <#29306#>(filter<#29306#> <#29307#>predicate?<#29307#> <#29308#>alon)<#29308#> 
  <#29309#>(c<#29309#><#29310#>ond<#29310#> 
    <#29311#>[<#29311#><#29312#>(empty?<#29312#> <#29313#>alon)<#29313#> <#29314#>empty]<#29314#> 
    <#29315#>[<#29315#><#29316#>else<#29316#> <#29317#>(c<#29317#><#29318#>ond<#29318#> 
            <#29319#>[<#29319#><#29320#>(predicate?<#29320#> <#29321#>(first<#29321#> <#29322#>alon))<#29322#> 
             <#29323#>(cons<#29323#> <#29324#>(first<#29324#> <#29325#>alon)<#29325#> <#29326#>(filter<#29326#> <#29327#>predicate?<#29327#> <#29328#>(rest<#29328#> <#29329#>alon)))]<#29329#> 
            <#29330#>[<#29330#><#29331#>else<#29331#> <#29332#>(filter<#29332#> <#29333#>predicate?<#29333#> <#29334#>(rest<#29334#> <#29335#>alon))]<#29335#><#29336#>)]<#29336#><#29337#>))<#29337#> 
Use <#65517#><#29341#>filter<#29341#><#65517#> to define the following functions:
  1. <#65518#><#29343#>eliminate-exp<#29343#><#65518#>, which consumes a number, <#65519#><#29344#>ua<#29344#><#65519#>, and a list of toy structures (containing name and price) and produces a list of all those descriptions whose price is below <#65520#><#29345#>ua<#29345#><#65520#>;
  2. <#65521#><#29346#>recall<#29346#><#65521#>, which consumes the name of a toy, called <#65522#><#29347#>ty<#29347#><#65522#>, and a list of names, called <#65523#><#29348#>lon<#29348#><#65523#>, and produces a list of names that contains all components of <#65524#><#29349#>lon<#29349#><#65524#> with the exception of <#65525#><#29350#>ty<#29350#><#65525#>;
  3. <#65526#><#29351#>selection<#29351#><#65526#>, which consumes two lists of names and selects all those from the second one that are also on the first.~ external Solution<#65527#><#65527#>