Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #reader(lib "htdp-intermediate-reader.ss" "lang")((modname 21.1.3) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp")))))
4 Exercise 21.1.3. Define natural-f, which is the abstraction of the following two functions:
6 ;; copy : N X -> (listof X)
7 ;; to create a list that contains
8 ;; obj n times
9 (define (copy n obj)
10 (cond
11 [(zero? n) empty]
12 [else (cons obj
13 (copy (sub1 n) obj))]))
15 ;; n-adder : N number -> number
16 ;; to add n to x using
17 ;; (+ 1 ...) only
18 (define (n-adder n x)
19 (cond
20 [(zero? n) x]
21 [else (+ 1
22 (n-adder (sub1 n) x))]))
23 Don't forget to test natural-f. Also use natural-f to define n-multiplier, which consumes n and x and produces n times x with additions only. Use the examples to formulate a contract.
24 Hint: The two function differ more than, say, the functions sum and product in exercise 21.1.2. In particular, the base case in one instance is a argument of the function, where in the other it is just a constant value. Solution