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 #|
5 12687dd9 2023-08-04 jrmu
6 12687dd9 2023-08-04 jrmu Exercise 21.1.3. Define natural-f, which is the abstraction of the following two functions:
7 12687dd9 2023-08-04 jrmu
8 12687dd9 2023-08-04 jrmu ;; copy : N X -> (listof X)
9 12687dd9 2023-08-04 jrmu ;; to create a list that contains
10 12687dd9 2023-08-04 jrmu ;; obj n times
11 12687dd9 2023-08-04 jrmu (define (copy n obj)
12 12687dd9 2023-08-04 jrmu (cond
13 12687dd9 2023-08-04 jrmu [(zero? n) empty]
14 12687dd9 2023-08-04 jrmu [else (cons obj
15 12687dd9 2023-08-04 jrmu (copy (sub1 n) obj))]))
16 12687dd9 2023-08-04 jrmu
17 12687dd9 2023-08-04 jrmu ;; n-adder : N number -> number
18 12687dd9 2023-08-04 jrmu ;; to add n to x using
19 12687dd9 2023-08-04 jrmu ;; (+ 1 ...) only
20 12687dd9 2023-08-04 jrmu (define (n-adder n x)
21 12687dd9 2023-08-04 jrmu (cond
22 12687dd9 2023-08-04 jrmu [(zero? n) x]
23 12687dd9 2023-08-04 jrmu [else (+ 1
24 12687dd9 2023-08-04 jrmu (n-adder (sub1 n) x))]))
25 12687dd9 2023-08-04 jrmu
26 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.
27 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
28 12687dd9 2023-08-04 jrmu
29 12687dd9 2023-08-04 jrmu |#
30 12687dd9 2023-08-04 jrmu
31 12687dd9 2023-08-04 jrmu ;natural-f : (X Z -> Z) N[>=0] X Y -> Z
32 12687dd9 2023-08-04 jrmu ;Given a function f, natural number n (N[>=0]), data (X), and base (Y), recursively use f on x, n times.
33 12687dd9 2023-08-04 jrmu (define (natural-f f n x base)
34 12687dd9 2023-08-04 jrmu (cond
35 12687dd9 2023-08-04 jrmu [(zero? n) base]
36 12687dd9 2023-08-04 jrmu [else (f x (natural-f f (sub1 n) x base))]))
37 12687dd9 2023-08-04 jrmu
38 12687dd9 2023-08-04 jrmu ;; copy : N X -> (listof X)
39 12687dd9 2023-08-04 jrmu ;; to create a list that contains
40 12687dd9 2023-08-04 jrmu ;; obj n times
41 12687dd9 2023-08-04 jrmu
42 12687dd9 2023-08-04 jrmu (define (copy n obj)
43 12687dd9 2023-08-04 jrmu (natural-f cons n obj empty))
44 12687dd9 2023-08-04 jrmu
45 12687dd9 2023-08-04 jrmu ;; n-adder : N number -> number
46 12687dd9 2023-08-04 jrmu ;; to add n to x using
47 12687dd9 2023-08-04 jrmu ;; (+ 1 ...) only
48 12687dd9 2023-08-04 jrmu
49 12687dd9 2023-08-04 jrmu (define (n-adder n x)
50 12687dd9 2023-08-04 jrmu (natural-f + n 1 x))
51 12687dd9 2023-08-04 jrmu
52 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.
53 12687dd9 2023-08-04 jrmu ;
54 12687dd9 2023-08-04 jrmu ;n-multiplier : N number -> number
55 12687dd9 2023-08-04 jrmu ;Multiply x by n with additions only.
56 12687dd9 2023-08-04 jrmu
57 12687dd9 2023-08-04 jrmu (define (n-multiplier n x)
58 12687dd9 2023-08-04 jrmu (natural-f + n x 0))
59 12687dd9 2023-08-04 jrmu
60 12687dd9 2023-08-04 jrmu (equal? empty (copy 0 'hi))
61 12687dd9 2023-08-04 jrmu (equal? '(hi hi hi) (copy 3 'hi))
62 12687dd9 2023-08-04 jrmu
63 12687dd9 2023-08-04 jrmu (equal? 35 (n-adder 10 25))
64 12687dd9 2023-08-04 jrmu (equal? 57 (n-adder 23 34))
65 12687dd9 2023-08-04 jrmu (equal? 32 (n-multiplier 8 4))