<span style="font-size:small"><span><span><span>If function &quot;natural-f&quot; --</span></span></span></span><div><span style="font-size:small"><span><span><span><br>
</span>
</span><span>
</span></span></span></div><div><div><span style="font-size:small"><span><span><span>;apply function f to x n times with a base case of TH where n=0</span></span></span></span></div>


<div><span style="font-size:small"><span><span><span>;(check-expect (natural-f cons &#39;x empty 5) (list &#39;x &#39;x &#39;x &#39;x &#39;x))</span></span></span></span></div>

<div><span style="font-size:small"><span><span><span>;(check-expect (natural-f + 1 5 9) 14)</span></span></span></span></div><div><span style="font-size:small"><span><span><span>(define (natural-f f x TH n)</span></span></span></span></div>



<div><span style="font-size:small"><span><span><span>&nbsp;&nbsp;(cond</span></span></span></span></div><div><span style="font-size:small"><span><span><span>&nbsp;&nbsp; &nbsp;[(zero? n) TH]</span></span></span></span></div>


<div><span style="font-size:small"><span><span><span>&nbsp;&nbsp; &nbsp;[else (f x</span></span></span></span></div><div><span style="font-size:small"><span><span><span>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (natural-f f x TH (sub1 n) ))]))</span></span></span></span></div>



<div><span><span><span><br></span></span></span></div><div><span><span><span>-- properly abstracts over the the functions&nbsp;</span></span></span></div>


<div><span style="color:rgb(0, 128, 128)"><span><br></span></span><span style="font-size:16px"><table><tbody><tr><td><div align="left"><pre style="margin-left:2em;color:rgb(165, 42, 42)"><span style="color:teal"><span style="font-weight:bold"><span style="font-size:small"><span><span>;; </span></span></span></span><code style="color:rgb(165, 42, 42)"><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>copy</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:green"><span style="font-weight:bold"><span style="font-size:small"><span><span>:</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><strong><span style="font-size:small"><span><span>N</span></span></span></strong><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>X</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> &nbsp;</span></span></span></span><tt><span style="font-weight:bold"><span style="font-size:small"><span><span>-&gt;</span></span></span></span></tt><span style="font-weight:bold"><span style="font-size:small"><span><span>&nbsp; (</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>listof</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>X</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>)</span></span></span></span></code></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
</span></span></span></span><span style="color:teal"><span style="font-weight:bold"><span style="font-size:small"><span><span>;; to create a list that contains</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
</span></span></span></span><span style="color:teal"><span style="font-weight:bold"><span style="font-size:small"><span><span>;; </span></span></span></span><code style="color:rgb(165, 42, 42)"><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>obj</span></span></span></span></span></code><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><code style="color:rgb(165, 42, 42)"><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span></code><span style="font-weight:bold"><span style="font-size:small"><span><span> times</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
(</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>define</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> (</span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>copy</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>obj</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>)
  (</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>cond</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
    [(</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>zero?</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>) </span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>empty</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>]
    [</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>else</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> (</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>cons</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>obj</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> 
                (</span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>copy</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> (</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>sub1</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>) </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>obj</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>))]))
</span></span></span></span></pre></div></td><td><span style="font-weight:bold"><span style="font-size:small"><span><span>&nbsp;</span></span></span></span></td>

<td><div align="left"><pre style="margin-left:2em;color:rgb(165, 42, 42)"><span style="color:teal"><span style="font-weight:bold"><span style="font-size:small"><span><span>;; </span></span></span></span><code style="color:rgb(165, 42, 42)"><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n-adder</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:green"><span style="font-weight:bold"><span style="font-size:small"><span><span>:</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><strong><span style="font-size:small"><span><span>N</span></span></span></strong><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>number</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> &nbsp;</span></span></span></span><tt><span style="font-weight:bold"><span style="font-size:small"><span><span>-&gt;</span></span></span></span></tt><span style="font-weight:bold"><span style="font-size:small"><span><span>&nbsp; </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>number</span></span></span></span></span></code></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
</span></span></span></span><span style="color:teal"><span style="font-weight:bold"><span style="font-size:small"><span><span>;; to add </span></span></span></span><code style="color:rgb(165, 42, 42)"><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span></code><span style="font-weight:bold"><span style="font-size:small"><span><span> to </span></span></span></span><code style="color:rgb(165, 42, 42)"><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>x</span></span></span></span></span></code><span style="font-weight:bold"><span style="font-size:small"><span><span> using</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
</span></span></span></span><span style="color:teal"><span style="font-weight:bold"><span style="font-size:small"><span><span>;; </span></span></span></span><code style="color:rgb(165, 42, 42)"><span style="font-weight:bold"><span style="font-size:small"><span><span>(</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>+</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:green"><span style="font-weight:bold"><span style="font-size:small"><span><span>1</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> ...)</span></span></span></span></code><span style="font-weight:bold"><span style="font-size:small"><span><span> only</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
(</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>define</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> (</span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n-adder</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>x</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>)
  (</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>cond</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
    [(</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>zero?</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>) </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>x</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>]
    [</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>else</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> (</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>+</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:green"><span style="font-weight:bold"><span style="font-size:small"><span><span>1</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
             (</span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n-adder</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> (</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>sub1</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>) </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>x</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>))]))</span></span></span></span></pre>




</div></td></tr></tbody></table></span></div><div><span><span style="font-size:small"><span><span>--then what is the general contract for &quot;natural-f&quot;?</span></span></span></span></div>


<div><span><span style="font-size:small"><span><span><br></span></span></span></span></div><div><span><span style="font-size:small"><span><span>Considering &quot;natural-f&quot; as an abstraction of function &quot;n-adder&quot; above, the resulting contract would be:</span></span></span></span></div>




<div><span><span style="font-size:small"><span style="text-decoration:underline"><span><span>(number number -&gt; number) number number number -&gt; number&nbsp;</span></span></span></span></span><span style="font-size:small"><span><span><br>

</span>
</span><span>
</span></span></div><div><span><span>(ex.&nbsp;(check-expect (natural-f + 1 5 9) 14)</span></span></div><div><span style="font-size:16px"><div><span style="font-size:small"><span><span><br>
</span>
</span></span></div><div><span style="font-size:small"><span><span>But what would the contract for &quot;natural-f&quot; be if it was viewed as an abstraction of function &quot;copy?&quot;</span></span></span></div>

<div><span style="font-size:13px"><span><span>(ex.(check-expect (natural-f cons &#39;x empty 5) (list &#39;x &#39;x &#39;x &#39;x &#39;x))</span></span></span></div>

<div><span style="font-size:small"><span><span><br></span></span></span></div><div><span style="font-size:small"><span><span>In that case, argument &quot;f&quot; would take the value of &quot;cons,&quot; so the question (for me) comes down to how to represent the primitive &quot;cons&quot; as a function type within a contract.&nbsp;</span></span></span></div>




<div><span style="font-size:small"><span><span><br></span></span></span></div><div><span style="font-size:small"><span><span>Is &quot;cons&quot; represented as (X Y -&gt; Y)</span></span></span></div>

<div><span style="font-size:small"><span><span>where X is any Scheme ITEM, Y is the Scheme-value list, and the result is basic type Y (Scheme-value list)?</span></span></span></div>



<div><span style="font-size:small"><span><span><br></span></span></span></div><div><span style="font-size:small"><span><span>Or is &quot;cons&quot; represented as &nbsp;(X list -&gt; (listof X)</span></span></span></div>

<div>
<span style="font-size:13px"><span><span>where X is any Scheme ITEM, &quot;list&quot; is Scheme-value list, and the result is defined type (listof X)?</span></span></span></div>

<div><span style="font-size:13px"><span><span><br></span></span></span></div><div><span style="font-size:13px"><span><span>The difference between the two contracts appears to be in the output types. &nbsp;The first output type is basic, which is consistent with the contract output for &quot;n-adder&quot;; the second output type is defined, which conflicts with &quot;n-adder.&quot; &nbsp;Avoiding conflict means choosing the first representation of &quot;cons&quot; in place of f in &quot;natural-f&quot;&#39;s contract. S my guess is that the proper general contract for &quot;natural-f&quot; would be:</span></span></span></div>


<div><span style="font-size:13px"><span><span><br></span></span></span></div><div><span style="font-size:13px"><span style="font-weight:bold"><span><span>;natural-f: (X Y -&gt; Y) X Y number -&gt; Y</span></span></span><span><span><br>

</span>
</span></span></div><div><span style="font-size:13px;font-weight:bold"><span><span><br></span></span></span></div><div><span style="font-size:13px"><span><span>Is this right? If not, where am I going wrong?</span></span></span></div>


<div><span style="font-size:13px"><span><span><br></span></span></span></div><div><span style="font-size:13px"><span><span>Thanks,</span></span></span></div>

<div><span style="font-size:13px"><span><span>Dave&nbsp;</span></span></span></div><div><br></div><div>&nbsp;</div><div><br></div><div><br>
</div><div><br></div><div><br></div></span></div><div><br></div><div><br>
</div><div><span style="font-size:16px"><br></span></div><div><span style="font-size:16px"><br>
</span><div align="left"><pre style="margin-left:2em;color:rgb(165, 42, 42)"></pre></div></div></div>