-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path04-1-lists.rkt
executable file
·183 lines (143 loc) · 5.26 KB
/
04-1-lists.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname 04-1-lists) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
;; lists.rkt
(require rackunit)
(require "extras.rkt")
;; A List of Numbers (LON) is one of:
;; -- empty
;; -- (cons Number LON)
;; Template:
;; ;; lon-fn : LON -> ??
;; (define (lon-fn lst)
;; (cond
;; [(empty? lst) ...]
;; [else (... (first lst)
;; (lon-fn (rest lst)))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; examples of list calculations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(begin-for-test
(check-equal? (empty? empty) true)
(check-equal? (empty? (cons 11 empty)) false)
(check-equal? (empty? (cons 22 (cons 11 empty))) false)
(check-equal? (first (cons 11 empty)) 11)
(check-equal? (rest (cons 11 empty)) empty)
(check-equal? (first (cons 22 (cons 11 empty))) 22)
(check-equal? (rest (cons 22 (cons 11 empty))) (cons 11 empty))
(check-error (first empty)
"first: expected argument of type <non-empty list>; given empty")
(check-error (rest empty)
"rest: expected argument of type <non-empty list>; given empty")
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; lon-length : LON -> Integer
;; GIVEN: a LON
;; RETURNS: its length
;; If the tests are simple, you can use them as examples.
;; But they'd better be very very simple.
(begin-for-test
(check-equal? (lon-length empty) 0)
(check-equal? (lon-length (cons 11 empty)) 1)
(check-equal? (lon-length (cons 33 (cons 11 empty))) 2))
; STRATEGY: Use template for LON on lst
(define (lon-length lst)
(cond
[(empty? lst) 0]
[else (+ 1
(lon-length (rest lst)))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; lon-sum : LON -> Number
;; GIVEN: a LON
;; RETURNS: its sum
;; EXAMPLES/TESTS:
(begin-for-test
(check-equal? (lon-sum empty) 0)
(check-equal? (lon-sum (cons 11 empty)) 11)
(check-equal? (lon-sum (cons 33 (cons 11 empty))) 44)
(check-equal? (lon-sum (cons 10 (cons 20 (cons 3 empty)))) 33))
; STRATEGY: Use template for LON on lst
(define (lon-sum lst)
(cond
[(empty? lst) 0]
[else (+ (first lst)
(lon-sum (rest lst)))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; lon-avg : LON -> Number
;; GIVEN: a non-empty LON
;; RETURNS: its average
(begin-for-test
"lon-avg tests"
(check-equal? (lon-avg (cons 11 empty)) 11)
(check-equal? (lon-avg (cons 33 (cons 11 empty))) 22)
(check-equal? (lon-avg (cons 10 (cons 20 (cons 3 empty)))) 11))
; STRATEGY: combine simpler functions (!)
(define (lon-avg lst)
(/ (lon-sum lst) (lon-length lst)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; double-all : LON -> LON
;; GIVEN: a LON
;; RETURNS: a list just like the original, but with each
;; number doubled
(begin-for-test
(check-equal? (double-all empty) empty)
(check-equal? (double-all (cons 11 empty)) (cons 22 empty))
(check-equal?
(double-all (cons 33 (cons 11 empty)))
(cons 66 (cons 22 empty))))
; strategy: Use template for LON on lst
(define (double-all lst)
(cond
[(empty? lst) empty]
[else (cons (* 2 (first lst))
(double-all (rest lst)))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; remove-evens : LON -> LON
;; GIVEN: a LON
;; RETURNS: a list just like the original, but with all
;; the even numbers removed
(define list-22-11-13-46-7
(cons 22
(cons 11 (cons 13 (cons 46 (cons 7 empty))))))
;; a list whose first even is not in the first position (not on the slides!)
(define list-17-22-11-13-46-7
(cons 17 list-22-11-13-46-7))
(begin-for-test
(check-equal? (remove-evens empty) empty)
(check-equal? (remove-evens (cons 11 empty)) (cons 11 empty))
(check-equal?
(remove-evens list-22-11-13-46-7)
(cons 11 (cons 13 (cons 7 empty))))
(check-equal?
(remove-evens list-17-22-11-13-46-7)
(cons 17 (cons 11 (cons 13 (cons 7 empty))))))
;; STRATEGY: Use template for LON on lst
(define (remove-evens lst)
(cond
[(empty? lst) empty]
[else (if (even? (first lst))
(remove-evens (rest lst))
(cons (first lst)
(remove-evens (rest lst))))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; remove-first-even : LON -> LON
;; GIVEN: a LON
;; RETURNS: a list just like the original, but with the first even
;; number, if any, removed.
(begin-for-test
(check-equal? (remove-first-even empty) empty)
(check-equal? (remove-first-even (cons 11 empty)) (cons 11 empty))
(check-equal? (remove-first-even list-22-11-13-46-7)
(cons 11 (cons 13 (cons 46 (cons 7 empty)))))
(check-equal?
(remove-first-even list-17-22-11-13-46-7)
(cons 17 (cons 11 (cons 13 (cons 46 (cons 7 empty)))))))
;; STRATEGY: Use template for LON on lst
(define (remove-first-even lst)
(cond
[(empty? lst) empty]
[else (if (even? (first lst))
(rest lst)
(cons (first lst)
(remove-first-even
(rest lst))))]))