<#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>
<#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>
<#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.~ Solution<#62919#><#62919#> <#13783#>Exercise 11.5.3<#13783#>
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.~ Solution<#62928#><#62928#>
<#13800#>Exercise 11.5.4<#13800#>
<#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#>