<span style="font-size:small"><span><span><span>If function "natural-f" --</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 'x empty 5) (list 'x 'x 'x 'x '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> (cond</span></span></span></span></div><div><span style="font-size:small"><span><span><span> [(zero? n) TH]</span></span></span></span></div>
<div><span style="font-size:small"><span><span><span> [else (f x</span></span></span></span></div><div><span style="font-size:small"><span><span><span> (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 </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> </span></span></span></span><tt><span style="font-weight:bold"><span style="font-size:small"><span><span>-></span></span></span></span></tt><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>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> </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> </span></span></span></span><tt><span style="font-weight:bold"><span style="font-size:small"><span><span>-></span></span></span></span></tt><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></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 "natural-f"?</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 "natural-f" as an abstraction of function "n-adder" 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 -> number) number number number -> number </span></span></span></span></span><span style="font-size:small"><span><span><br>
</span>
</span><span>
</span></span></div><div><span><span>(ex. (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 "natural-f" be if it was viewed as an abstraction of function "copy?"</span></span></span></div>
<div><span style="font-size:13px"><span><span>(ex.(check-expect (natural-f cons 'x empty 5) (list 'x 'x 'x 'x '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 "f" would take the value of "cons," so the question (for me) comes down to how to represent the primitive "cons" as a function type within a contract. </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 "cons" represented as (X Y -> 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 "cons" represented as (X list -> (listof X)</span></span></span></div>
<div>
<span style="font-size:13px"><span><span>where X is any Scheme ITEM, "list" 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. The first output type is basic, which is consistent with the contract output for "n-adder"; the second output type is defined, which conflicts with "n-adder." Avoiding conflict means choosing the first representation of "cons" in place of f in "natural-f"'s contract. S my guess is that the proper general contract for "natural-f" 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 -> Y) X Y number -> 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 </span></span></span></div><div><br></div><div> </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>