Function Simplification

The <#64250#><#20734#>list-pick<#20734#><#64250#> function in figure~#figlistpick#20735> is more complicated than necessary. Both the first and the second <#64251#><#20736#>cond<#20736#><#64251#>-clause produce the same answer: <#64252#><#20737#>false<#20737#><#64252#>. In other words, if either
<#20742#>(and<#20742#> <#20743#>(=<#20743#> <#20744#>n<#20744#> <#20745#>1)<#20745#> <#20746#>(empty?<#20746#> <#20747#>alos))<#20747#>
or
<#20755#>(and<#20755#> <#20756#>(;SPMgt;<#20756#> <#20757#>n<#20757#> <#20758#>1)<#20758#> <#20759#>(empty?<#20759#> <#20760#>alos))<#20760#>
evaluates to <#64253#><#20764#>true<#20764#><#64253#>, the answer is <#64254#><#20765#>false<#20765#><#64254#>. We can translate this observation into a simpler <#64255#><#20766#>cond<#20766#>-expression<#64255#>:
<#20771#>(d<#20771#><#20772#>efine<#20772#> <#20773#>(list-pick<#20773#> <#20774#>n<#20774#> <#20775#>alos)<#20775#>
  <#20776#>(c<#20776#><#20777#>ond<#20777#> 
    <#20778#>[<#20778#><#20779#>(or<#20779#> <#20780#>(and<#20780#> <#20781#>(=<#20781#> <#20782#>n<#20782#> <#20783#>1)<#20783#> <#20784#>(empty?<#20784#> <#20785#>alos))<#20785#> 
         <#20786#>(and<#20786#> <#20787#>(;SPMgt;<#20787#> <#20788#>n<#20788#> <#20789#>1)<#20789#> <#20790#>(empty?<#20790#> <#20791#>alos)))<#20791#> <#20792#>false<#20792#><#20793#>]<#20793#> 
    <#20794#>[<#20794#><#20795#>(and<#20795#> <#20796#>(=<#20796#> <#20797#>n<#20797#> <#20798#>1)<#20798#> <#20799#>(cons?<#20799#> <#20800#>alos))<#20800#> <#20801#>(first<#20801#> <#20802#>alos)]<#20802#> 
    <#20803#>[<#20803#><#20804#>(and<#20804#> <#20805#>(;SPMgt;<#20805#> <#20806#>n<#20806#> <#20807#>1)<#20807#> <#20808#>(cons?<#20808#> <#20809#>alos))<#20809#> <#20810#>(list-pick<#20810#> <#20811#>(rest<#20811#> <#20812#>alos)<#20812#> <#20813#>(sub1<#20813#> <#20814#>n))]<#20814#><#20815#>))<#20815#> 
The new expression is a direct transliteration of our English observation. To simplify this function even more, we need to get acquainted with an algebraic law concerning booleans:
  <#20823#>(or<#20823#> <#20824#>(and<#20824#> <#20825#>condition1<#20825#> <#20826#>a-condition)<#20826#> 
      <#20827#>(and<#20827#> <#20828#>condition2<#20828#> <#20829#>a-condition))<#20829#> 
<#20830#>=<#20830#> <#20831#>(and<#20831#> <#20832#>a-condition<#20832#> 
       <#20833#>(or<#20833#> <#20834#>condition1<#20834#> <#20835#>condition2))<#20835#> 
The law is called de Morgan's law of distributivity. Applying it to our function yields the following:
<#20843#>(d<#20843#><#20844#>efine<#20844#> <#20845#>(list-pick<#20845#> <#20846#>n<#20846#> <#20847#>alos)<#20847#>
  <#20848#>(c<#20848#><#20849#>ond<#20849#> 
    <#20850#>[<#20850#><#20851#>(and<#20851#> <#20852#>(empty?<#20852#> <#20853#>alos)<#20853#> 
          <#20854#>(or<#20854#> <#20855#>(=<#20855#> <#20856#>n<#20856#> <#20857#>1)<#20857#> <#20858#>(;SPMgt;<#20858#> <#20859#>n<#20859#> <#20860#>1)))<#20860#> <#20861#>false<#20861#><#20862#>]<#20862#> 
    <#20863#>[<#20863#><#20864#>(and<#20864#> <#20865#>(=<#20865#> <#20866#>n<#20866#> <#20867#>1)<#20867#> <#20868#>(cons?<#20868#> <#20869#>alos))<#20869#> <#20870#>(first<#20870#> <#20871#>alos)]<#20871#> 
    <#20872#>[<#20872#><#20873#>(and<#20873#> <#20874#>(;SPMgt;<#20874#> <#20875#>n<#20875#> <#20876#>1)<#20876#> <#20877#>(cons?<#20877#> <#20878#>alos))<#20878#> <#20879#>(list-pick<#20879#> <#20880#>(rest<#20880#> <#20881#>alos)<#20881#> <#20882#>(sub1<#20882#> <#20883#>n))]<#20883#><#20884#>))<#20884#> 
That is, if <#64256#><#20888#>alos<#20888#><#64256#> is <#64257#><#20889#>empty<#20889#><#64257#> and <#64258#><#20890#>n<#20890#><#64258#> is either <#64259#><#20891#>1<#20891#><#64259#> or greater than <#64260#><#20892#>1<#20892#><#64260#>, the answer is <#64261#><#20893#>false<#20893#><#64261#>. Now consider the second part of the condition: <#64262#><#20894#>(or<#20894#>\ <#20895#>(=<#20895#>\ <#20896#>n<#20896#>\ <#20897#>1)<#20897#>\ <#20898#>(;SPMgt;<#20898#>\ <#20899#>n<#20899#><#20900#> <#20900#><#20901#>1))<#20901#><#64262#>. Because <#64263#><#20902#>n<#20902#><#64263#> belongs to <#64264#><#20903#>N<#20903#><#20904#>[<#20904#><#20905#>;SPMgt;=<#20905#>\ <#20906#>1]<#20906#><#64264#>, the condition is always true. But, if we replace it we true we get
<#20911#>(and<#20911#> <#20912#>(empty?<#20912#> <#20913#>alos)<#20913#>
     <#20914#>true)<#20914#>      
which is clearly equivalent to <#64265#><#20918#>(empty?<#20918#>\ <#20919#>alos)<#20919#><#64265#>. In other words, the function can be written as
<#20924#>(d<#20924#><#20925#>efine<#20925#> <#20926#>(list-pick<#20926#> <#20927#>n<#20927#> <#20928#>alos)<#20928#>
  <#20929#>(c<#20929#><#20930#>ond<#20930#> 
    <#20931#>[<#20931#><#20932#>(empty?<#20932#> <#20933#>alos)<#20933#> <#20934#>false<#20934#><#20935#>]<#20935#> 
    <#20936#>[<#20936#><#20937#>(and<#20937#> <#20938#>(=<#20938#> <#20939#>n<#20939#> <#20940#>1)<#20940#> <#20941#>(cons?<#20941#> <#20942#>alos))<#20942#> <#20943#>(first<#20943#> <#20944#>alos)]<#20944#> 
    <#20945#>[<#20945#><#20946#>(and<#20946#> <#20947#>(;SPMgt;<#20947#> <#20948#>n<#20948#> <#20949#>1)<#20949#> <#20950#>(cons?<#20950#> <#20951#>alos))<#20951#> <#20952#>(list-pick<#20952#> <#20953#>(rest<#20953#> <#20954#>alos)<#20954#> <#20955#>(sub1<#20955#> <#20956#>n))]<#20956#><#20957#>))<#20957#> 
which is already significantly simpler than that in figure~#figlistpick#20961>. Still, we can do even better than that. The first condition in the latest version of <#64266#><#20962#>list-pick<#20962#><#64266#> filters out all those cases when <#64267#><#20963#>alos<#20963#><#64267#> is empty. Hence, <#64268#><#20964#>(cons?<#20964#>\ <#20965#>alos)<#20965#><#64268#> in the next two clauses is always going to evaluate to <#64269#><#20966#>true<#20966#><#64269#>. If we replace the condition by <#64270#><#20967#>true<#20967#><#64270#> and simplify the <#64271#><#20968#>and<#20968#>-expression<#64271#>s, we get the simplest possible version of <#64272#><#20969#>list-pick<#20969#><#64272#>, which is displayed in figure~#figlistpick2#20970>. While this last function is simpler than the original, it is important to understand that we designed both the original and the simplified version in a systematic manner and that we can therefore trust both. If we try to find the simple versions directly, we sooner or later fail to consider a case and produce flawed functions.
<#71146#>;; <#64273#><#20975#>list-pick<#20975#> <#20976#>:<#20976#> <#20977#>list-of-symbols<#20977#> <#20978#>N<#20978#><#20979#>[<#20979#><#20980#>;SPMgt;=<#20980#> <#20981#>1]<#20981#> <#20982#><#20982#><#20983#>-;SPMgt;<#20983#><#20984#><#20984#> <#20985#>symbol<#20985#><#64273#><#71146#>
<#71147#>;; to determine the <#64274#><#20986#>n<#20986#><#64274#>th symbol from <#64275#><#20987#>alos<#20987#><#64275#>, counting from <#64276#><#20988#>1<#20988#><#64276#>;<#71147#> 
<#71148#>;; signals an error if there is no <#64277#><#20989#>n<#20989#><#64277#>th item<#71148#> 
<#20990#>(d<#20990#><#20991#>efine<#20991#> <#20992#>(list-pick<#20992#> <#20993#>n<#20993#> <#20994#>alos)<#20994#> 
  <#20995#>(c<#20995#><#20996#>ond<#20996#> 
    <#20997#>[<#20997#><#20998#>(empty?<#20998#> <#20999#>alos)<#20999#> <#21000#>(error<#21000#> <#21001#>'<#21001#><#21002#>list-pick<#21002#> <#21003#>``list<#21003#> <#21004#>too<#21004#> <#21005#>short'')]<#21005#> 
    <#21006#>[<#21006#><#21007#>(=<#21007#> <#21008#>n<#21008#> <#21009#>1)<#21009#> <#21010#>(first<#21010#> <#21011#>alos)]<#21011#> 
    <#21012#>[<#21012#><#21013#>(;SPMgt;<#21013#> <#21014#>n<#21014#> <#21015#>1)<#21015#> <#21016#>(list-pick<#21016#> <#21017#>(rest<#21017#> <#21018#>alos)<#21018#> <#21019#>(sub1<#21019#> <#21020#>n))]<#21020#><#21021#>))<#21021#> 
<#64278#>Figure: The simplified definition of <#21025#>list-pick<#21025#><#64278#>

<#21029#>Exercise 17.4.1<#21029#> Develop the function <#64279#><#21031#>replace-eol-with<#21031#><#64279#> following the strategy of section~#sectwoinputscase2#21032>. Then simplify it systematically.~ external Solution<#64280#><#64280#> <#21038#>Exercise 17.4.2<#21038#> Simplify the function <#64281#><#21040#>list-pick0<#21040#><#64281#> from exercise~#exlistref#21041> or explain why it can't be simplified.~ external Solution<#64282#><#64282#>