<div dir="ltr">The solution is missing, but I thought of exposing this one here so that it gets scrutinized before sending it to Robby Findler.<div><br></div><div><div>;; Exercise 21.1.3. Define natural-f, which is the abstraction of the</div><div>;; following two functions:</div><div>;; </div><div>;; ;; copy : N X  ->  (listof X)</div><div>;; ;; to create a list that contains</div><div>;; ;; obj n times</div><div>;; (define (copy n obj)</div><div>;;   (cond</div><div>;;     [(zero? n) empty]</div><div>;;     [else (cons obj </div><div>;;                 (copy (sub1 n) obj))]))</div><div>;;  <span class="" style="white-space:pre"> </span></div><div>;; ;; n-adder : N number  ->  number</div><div>;; ;; to add n to x using</div><div>;; ;; (+ 1 ...) only</div><div>;; (define (n-adder n x)</div><div>;;   (cond</div><div>;;     [(zero? n) x]</div><div>;;     [else (+ 1</div><div>;;              (n-adder (sub1 n) x))]))</div><div>;; </div><div>;; Don't forget to test natural-f. Also use natural-f to define</div><div>;; n-multiplier, which consumes n and x and produces n times x with</div><div>;; additions only. Use the examples to formulate a contract.</div><div>;; </div><div>;; Hint: The two function differ more than, say, the functions sum and</div><div>;; product in exercise 21.1.2. In particular, the base case in one</div><div>;; instance is a argument of the function, where in the other it is just</div><div>;; a constant value.</div><div>;; </div><div>;; (*) Solution</div><div>;; </div><div>;; After following the design recipe for abstraction, we get natural-f as</div><div>;; shown below. To write the contract, let's first only consider copy,</div><div>;; then we consider only n-adder and then we consider both.</div><div>;; </div><div>;; ;; natural-f-copy: Nat X (X (listof X) -> (listof X)) (listof X) -> (listof X)</div><div>;; ;; natural-f-adder: Nat number (number number -> number) number -> number</div><div>;; </div><div>;; The contract for natural-f-adder is more specific than</div><div>;; natural-f-copy. The difference between these is that in the first, two</div><div>;; different types are involved --- X and (listof X). Therefore, (listof</div><div>;; X) works as another variable, so let's call it Y in the final version.</div><div> </div><div>;; natural-f: Nat X (X Y -> Y) Y -> Y</div><div>(define (natural-f n x f init)</div><div>  (cond</div><div>    [(zero? n) init]</div><div>    [else (f x</div><div>              (natural-f (sub1 n) x f init))]))</div><div><br></div><div>;; (*) Tests</div><div><br></div><div>;; Let's use two different types in copy, say symbol and then number.</div><div><br></div><div>(check-expect (copy 3 'a) '(a a a))</div><div>(check-expect (natural-f 3 'a cons empty) (copy 3 'a))</div><div><br></div><div>(check-expect (copy 0 'a) empty)</div><div>(check-expect (natural-f 0 'a cons empty) (copy 0 'a))</div><div><br></div><div>(check-expect (copy 3 1) (list 1 1 1))</div><div>(check-expect (natural-f 3 1 cons empty) (copy 3 1))</div><div><br></div><div>;; For n-adder, let's make sure to test different init-values.</div><div><br></div><div>(check-expect (n-adder 3 3.14) 6.14)</div><div>(check-expect (natural-f 3 1 + 3.14) (n-adder 3 3.14))</div><div><br></div><div>(check-expect (n-adder 3 1) 4)</div><div>(check-expect (natural-f 3 1 + 1) (n-adder 3 1))</div><div><br></div><div>(check-expect (n-adder 3 2) 5)</div><div>(check-expect (natural-f 3 1 + 2) (n-adder 3 2))</div></div><div><br></div></div>