More on the Nature of Natural Numbers

The natural numbers are a small subset of Scheme's numbers, not all of them. Hence, the function template above <#13630#>cannot<#13630#> be used for processing arbitrary numbers, <#13631#>e.g.<#13631#>, inexact numbers. Still, the template is a good starting point for functions whose definitions involve both natural numbers and other Scheme numbers. To illustrate this point, let us design the function <#62882#><#13632#>add-to-pi<#13632#><#62882#>, which consumes a natural number <#62883#><#13633#>n<#13633#><#62883#> and produces <#62884#><#13634#>n<#13634#>\ <#13635#>+<#13635#>\ <#13636#>3.14<#13636#><#62884#> without using <#62885#><#13637#>+<#13637#><#62885#>. Following the design recipe, we start with
<#71013#>;; <#62886#><#13642#>add-to-pi<#13642#> <#13643#>:<#13643#> <#13644#>N<#13644#> <#13645#><#13645#><#13646#>-;SPMgt;<#13646#><#13647#><#13647#> <#13648#>number<#13648#><#62886#><#71013#>
<#71014#>;; to compute n+3.14 without using <#62887#><#13649#>+<#13649#><#62887#><#71014#> 
<#13650#>(define<#13650#> <#13651#>(add-to-pi<#13651#> <#13652#>n)<#13652#> <#13653#>...)<#13653#> 
Another easy step is to determine the output for a few sample inputs:
<#13661#>(add-to-pi<#13661#> <#13662#>0)<#13662#> <#13663#>=<#13663#> <#13664#>3.14<#13664#>
<#13665#>(add-to-pi<#13665#> <#13666#>2)<#13666#> <#13667#>=<#13667#> <#13668#>5.14<#13668#> 
<#13669#>(add-to-pi<#13669#> <#13670#>6)<#13670#> <#13671#>=<#13671#> <#13672#>9.14<#13672#> 
The difference between <#62888#><#13676#>hellos<#13676#><#62888#>'s contract (see exercise~#excopy#13677>) and that of <#62889#><#13678#>add-to-pi<#13678#><#62889#> is the output, but as we have seen before this does not affect the template design. We obtain the template for <#62890#><#13679#>add-to-pi<#13679#><#62890#> by renaming <#62891#><#13680#>hellos<#13680#><#62891#> appropriately:
<#13685#>(d<#13685#><#13686#>efine<#13686#> <#13687#>(add-to-pi<#13687#> <#13688#>n)<#13688#>
  <#13689#>(c<#13689#><#13690#>ond<#13690#> 
    <#13691#>[<#13691#><#13692#>(zero?<#13692#> <#13693#>n)<#13693#> <#13694#>...]<#13694#> 
    <#13695#>[<#13695#><#13696#>else<#13696#> <#13697#>...<#13697#> <#13698#>(add-to-pi<#13698#> <#13699#>(sub1<#13699#> <#13700#>n))<#13700#> <#13701#>...<#13701#> <#13702#>]<#13702#><#13703#>)))<#13703#> 
In combination with the examples, the template immediately suggests how to complete the function. If the input is <#62892#><#13707#>0<#13707#><#62892#>, <#62893#><#13708#>add-to-pi<#13708#><#62893#>'s answer is <#62894#><#13709#>3.14<#13709#><#62894#>. Otherwise, <#62895#><#13710#>(add-to-pi<#13710#>\ <#13711#>(sub1<#13711#>\ <#13712#>n))<#13712#><#62895#> produces <#62896#><#13713#>(-<#13713#>\ <#13714#>n<#13714#>\ <#13715#>1)<#13715#>\ <#13716#>+<#13716#>\ <#13717#>3.14<#13717#><#62896#>; since the correct answer is <#62897#><#13718#>1<#13718#><#62897#> more than this value, the answer expression in the second <#62898#><#13719#>cond<#13719#><#62898#>-line is <#62899#><#13720#>(add1<#13720#>\ <#13721#>(add-to-pi<#13721#>\ <#13722#>(sub1<#13722#>\ <#13723#>n)))<#13723#><#62899#>. Figure~#figaddtopi#13724> contains the complete function definition.
<#13727#>Exercise 11.5.1<#13727#> Define <#62900#><#13729#>add<#13729#><#62900#>, which consumes two natural numbers, <#62901#><#13730#>n<#13730#><#62901#> and <#62902#><#13731#>x<#13731#><#62902#>, and produces <#62903#><#13732#>n<#13732#>\ <#13733#>+<#13733#>\ <#13734#>x<#13734#><#62903#> without using Scheme's <#62904#><#13735#>+<#13735#><#62904#>. external Solution<#62905#><#62905#> <#13741#>Exercise 11.5.2<#13741#> Develop the function <#62906#><#13743#>multiply-by-pi<#13743#><#62906#>, which consumes a natural number and multiplies it by <#62907#><#13744#>3.14<#13744#><#62907#> without using <#62908#><#13745#>*<#13745#><#62908#>. For example,
<#13750#>(multiply-by-pi<#13750#> <#13751#>0)<#13751#> <#13752#>=<#13752#> <#13753#>0<#13753#>
<#13754#>(multiply-by-pi<#13754#> <#13755#>2)<#13755#> <#13756#>=<#13756#> <#13757#>6.28<#13757#> 
<#13758#>(multiply-by-pi<#13758#> <#13759#>3)<#13759#> <#13760#>=<#13760#> <#13761#>9.42<#13761#> 
Define <#62909#><#13765#>multiply<#13765#><#62909#>, which consumes two natural numbers, <#62910#><#13766#>n<#13766#><#62910#> and <#62911#><#13767#>x<#13767#><#62911#>, and produces <#62912#><#13768#>n<#13768#>\ <#13769#>*<#13769#>\ <#13770#>x<#13770#><#62912#> without using Scheme's <#62913#><#13771#>*<#13771#><#62913#>. Eliminate <#62914#><#13772#>+<#13772#><#62914#> from these definitions, too. <#13773#>Hint:<#13773#> \ Recall that multipliplying <#62915#><#13774#>x<#13774#><#62915#> by <#62916#><#13775#>n<#13775#><#62916#> means adding <#62917#><#13776#>x<#13776#><#62917#> to itself <#62918#><#13777#>n<#13777#><#62918#> times.~ external Solution<#62919#><#62919#> <#13783#>Exercise 11.5.3<#13783#> Develop the function <#62920#><#13785#>exponent<#13785#><#62920#>, which consumes a natural number <#62921#><#13786#>n<#13786#><#62921#> and a number <#62922#><#13787#>x<#13787#><#62922#> and computes

#displaymath73046#

Eliminate <#62923#><#13789#>*<#13789#><#62923#> from the definition, too. <#13790#>Hint:<#13790#> \ Recall that exponentiating <#62924#><#13791#>x<#13791#><#62924#> by <#62925#><#13792#>n<#13792#><#62925#> means multiplying <#62926#><#13793#>x<#13793#><#62926#> with itself <#62927#><#13794#>n<#13794#><#62927#> times.~ external Solution<#62928#><#62928#> <#13800#>Exercise 11.5.4<#13800#> Deep lists (see exercise~#exdeep#13802>) are another representation for natural numbers. Show how to represent <#62929#><#13803#>0<#13803#><#62929#>, <#62930#><#13804#>3<#13804#><#62930#>, and <#62931#><#13805#>8<#13805#><#62931#>. Develop the function <#62932#><#13806#>addDL<#13806#><#62932#>, which consumes two deep lists, representing the natural numbers <#62933#><#13807#>n<#13807#><#62933#> and <#62934#><#13808#>m<#13808#><#62934#>, and which produces a deep list representing <#62935#><#13809#>n<#13809#>\ <#13810#>+<#13810#>\ <#13811#>m<#13811#><#62935#>. external Solution<#62936#><#62936#>


<#71015#>;; <#62937#><#13821#>add-to-pi<#13821#> <#13822#>:<#13822#> <#13823#>N<#13823#> <#13824#><#13824#><#13825#>-;SPMgt;<#13825#><#13826#><#13826#> <#13827#>number<#13827#><#62937#><#71015#>
<#71016#>;; to compute n+3.14 without using <#62938#><#13828#>+<#13828#><#62938#><#71016#> 
<#13829#>(d<#13829#><#13830#>efine<#13830#> <#13831#>(add-to-pi<#13831#> <#13832#>n)<#13832#> 
  <#13833#>(c<#13833#><#13834#>ond<#13834#> 
    <#13835#>[<#13835#><#13836#>(zero?<#13836#> <#13837#>n)<#13837#> <#13838#>3.14]<#13838#> 
    <#13839#>[<#13839#><#13840#>else<#13840#> <#13841#>(add1<#13841#> <#13842#>(add-to-pi<#13842#> <#13843#>(sub1<#13843#> <#13844#>n)))]<#13844#><#13845#>))<#13845#> 
<#13849#>Figure: Adding a natural number to pi<#13849#>