Blame


1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #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 12687dd9 2023-08-04 jrmu Exercise 21.1.3. Define natural-f, which is the abstraction of the following two functions:
5 12687dd9 2023-08-04 jrmu
6 12687dd9 2023-08-04 jrmu ;; copy : N X -> (listof X)
7 12687dd9 2023-08-04 jrmu ;; to create a list that contains
8 12687dd9 2023-08-04 jrmu ;; obj n times
9 12687dd9 2023-08-04 jrmu (define (copy n obj)
10 12687dd9 2023-08-04 jrmu (cond
11 12687dd9 2023-08-04 jrmu [(zero? n) empty]
12 12687dd9 2023-08-04 jrmu [else (cons obj
13 12687dd9 2023-08-04 jrmu (copy (sub1 n) obj))]))
14 12687dd9 2023-08-04 jrmu
15 12687dd9 2023-08-04 jrmu ;; n-adder : N number -> number
16 12687dd9 2023-08-04 jrmu ;; to add n to x using
17 12687dd9 2023-08-04 jrmu ;; (+ 1 ...) only
18 12687dd9 2023-08-04 jrmu (define (n-adder n x)
19 12687dd9 2023-08-04 jrmu (cond
20 12687dd9 2023-08-04 jrmu [(zero? n) x]
21 12687dd9 2023-08-04 jrmu [else (+ 1
22 12687dd9 2023-08-04 jrmu (n-adder (sub1 n) x))]))
23 12687dd9 2023-08-04 jrmu 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 12687dd9 2023-08-04 jrmu 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