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.2.1) (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 (local ((define (f x) x))
5 (build-list 4 f))
7 (local ((define (f x) (+ 1 x)))
8 (build-list 4 f))
10 (local ((define (f x)
11 (expt 10 (* -1
12 (+ x 1)))))
13 (build-list 4 f))
15 ;evens : N [>=0] -> (listof N [>=0])
16 ;Given n, evens creates a list of the first n even numbers.
18 (define (evens n)
19 (local ((define (evens n)
20 (build-list n f))
21 (define (f n)
22 (* 2
23 (+ 1 n))))
24 (evens n)))
26 #|
28 (define (tabulate op n)
29 (cond
30 [(= n 0) (list (op 0))]
31 [else (cons (op n)
32 (tabulate op (sub1 n)))]))
34 |#
36 ;tabulate : (number -> X) number -> (listof X)
37 ;Given op and n, tabulate a list of (f x) from x = n to x = 0.
39 (define (tabulate op n)
40 (reverse (build-list (+ n 1) op)))
42 ;diagonal : N [>=0] -> (listof (listof number))
43 ;Creates a list of list of numbers representing a square identity matrix of width and height n with 1's where the row index equals the column index and 0's elsewhere. Do this by building a list of length n with 1 in the n-th position and 0's elsewhere. Then, append this list to the previously formed diagonal with a column of 0's appended to it.
45 ;first-row : N [>=0] -> (listof number)
46 ;Given n, create the first row of the n-by-n identity matrix.
48 ;add-col-zero : (listof (listof number)) -> (listof (listof number))
49 ;Given a-diag, add a new column of zeros to the left side of a-diag.
51 (define (diagonal n)
52 (local ((define (diagonal n)
53 (cond
54 [(zero? n) empty]
55 [else (append (list (build-list n first-row))
56 (add-col-zero (diagonal (sub1 n))))]))
57 (define (first-row n)
58 (cond
59 [(= n 0) 1]
60 [else 0]))
61 (define (add-col-zero a-diag)
62 (cond
63 [(empty? a-diag) empty]
64 [else (cons (cons 0 (first a-diag))
65 (add-col-zero (rest a-diag)))])))
66 (diagonal n)))
68 #|
70 ;f : N[>=0] N[>=0] -> (listof number)
71 ;Creates a (listof number) with length l with 1 at the n-th position (the first position has an index of zero) and 0's elsewhere.
73 (define (f n l)
74 (cond
75 [(zero? l) empty]
76 [else ... (sub1 l) ...]))
79 (list
80 (list 1 0 0)
81 (list 0 1 0)
82 (list 0 0 1)))
85 (list
86 (list 1 0)
87 (list 0 1)))
89 |#
91 #|
92 Exercise 21.2.1. Use build-list
94 to define tabulate from exercise 21.1.1; and
96 to define diagonal, which consumes a natural number n and creates a list of lists of 0 and 1.
97 Example:
99 (equal? (diagonal 3)
100 (list
101 (list 1 0 0)
102 (list 0 1 0)
103 (list 0 0 1)))
104 Use local if function definitions require auxiliary functions. Solution
106 ;; build-list : N (N -> X) -> (listof X)
107 ;; to construct (list (f 0) ... (f (- n 1)))
108 (define (build-list n f) ...)