-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.rkt
238 lines (224 loc) · 6.31 KB
/
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#lang racket
;;; File:
;;; lists.rkt
;;; Summary:
;;; A variety of procedure associated with lists.
;;; Author:
;;; Samuel A. Rebelsky
(provide
(contract-out
[reduce (-> (-> any/c any/c any) list? any/c)]
[reduce-left (-> (-> any/c any/c any) list? any/c)]
[reduce-right (-> (-> any/c any/c any) list? any/c)]
[take-random (-> list? integer? list?)]
[tally (-> list? (-> any/c any) integer?)]
[tally-value (-> list any/c integer?)]
[range (-> integer? list?)]
[range1 (-> integer? list?)]
))
; +-------------------------------+----------------------------------
; | Private procedures and values |
; +-------------------------------+
;;; Constant:
;;; FIRST-FIRST-ODDS
;;; Type:
;;; real
;;; Summary:
;;; The odds that we process the first element first when mapping
;;; through a list.
(define FIRST-FIRST-ODDS 0.4)
; +---------------------+--------------------------------------------
; | Exported procedures |
; +---------------------+
;;; Package:
;;; loudhum/lists
;;; Procedure:
;;; reduce
;;; Parameters:
;;; op, a binary procedure
;;; lst, a list of values of the form (val1 val2 ... valn)
;;; Purpose:
;;; Creates a new value by repeatedly applying op to the values
;;; in lst.
;;; Produces:
;;; result, a value
;;; Preconditions:
;;; op must be applicable to pairs of elements of lst.
;;; op must return the same types that it takes as input
;;; Postconditions:
;;; In infix notation, result is val1 op val2 op val3 ... op valn
;;; the order of the evalution of the operations is undefined
(define reduce
(lambda (fun lst)
(cond
[(null? (cdr lst))
(car lst)]
[(< (random) FIRST-FIRST-ODDS)
(reduce fun (cons (fun (car lst) (cadr lst))
(cddr lst)))]
[else
(fun (car lst) (reduce fun (cdr lst)))])))
;;; Package:
;;; loudhum/lists
;;; Procedure:
;;; reduce-left
;;; Parameters:
;;; op, a binary procedure
;;; lst, a list of values of the form (val1 val2 ... valn)
;;; Purpose:
;;; Creates a new value by repeatedly applying op to the values
;;; in lst.
;;; Produces:
;;; result, a value
;;; Preconditions:
;;; op must be applicable to pairs of elements of lst.
;;; op must return the same types that it takes as input
;;; Postconditions:
;;; result is (op (op (op val1 val2) val3) ... valn)
(define reduce-left
(lambda (fun lst)
(cond
[(null? (cdr lst))
(car lst)]
[else
(reduce-left fun (cons (fun (car lst) (cadr lst))
(cddr lst)))])))
;;; Package:
;;; loudhum/lists
;;; Procedure:
;;; reduce-right
;;; Parameters:
;;; op, a binary procedure
;;; lst, a list of values of the form (val1 val2 ... valn)
;;; Purpose:
;;; Creates a new value by repeatedly applying op to the values
;;; in lst.
;;; Produces:
;;; result, a value
;;; Preconditions:
;;; op must be applicable to pairs of elements of lst.
;;; op must return the same types that it takes as input
;;; Postconditions:
;;; result is (op val1 (op val2 (op val3 ... (op valn-1 valn))))
(define reduce-right
(lambda (fun lst)
(cond
[(null? (cdr lst))
(car lst)]
[else
(fun (car lst) (reduce-right fun (cdr lst)))])))
;;; Procedure:
;;; take-random
;;; Parameters:
;;; lst, a list
;;; n, a non-negative integer
;;; Purpose:
;;; Grab n "randomly selected elements" from lst
;;; Produces:
;;; elements, a list
;;; Preconditions:
;;; n <= (length lst)
;;; Postconditions:
;;; (length elements) = n
;;; Every element in elements appears in lst.
;;; Every element in elements represents a separate element of lst.
(define take-random
(lambda (lst n)
(let kernel ([remaining lst]
[len (length lst)]
[n n])
(cond
[(or (= n 0) (= len 0))
null]
[(<= (random) (/ n len))
(cons (car remaining)
(kernel (cdr remaining)
(- len 1)
(- n 1)))]
[else
(kernel (cdr remaining)
(- len 1)
n)]))))
;;; Procedure:
;;; tally
;;; Parameters:
;;; lst, a list
;;; pred?, a unary predicate
;;; Purpose:
;;; Count the number of values in lst for which pred? holds.
;;; Produces:
;;; count, a non-negative integer
;;; Preconditions:
;;; pred? can be applied to all elements of lst.
;;; Postconditions:
;;; count represents the number of values in lst for which
;;; (pred? val) holds.
(define tally
(lambda (lst pred?)
(let kernel ([remaining lst]
[count 0])
(cond
[(null? remaining)
count]
[(pred? (car remaining))
(kernel (cdr remaining) (+ 1 count))]
[else
(kernel (cdr remaining) count)]))))
;;; Procedure:
;;; tally-value
;;; Parameters:
;;; lst, a list
;;; val, a value
;;; Purpose:
;;; Count the number values in lst equal to val
;;; Produces:
;;; count, a non-negative integer
;;; Preconditions:
;;; [No additional]
;;; Postconditions:
;;; count represents the number of values, v, in lst for which
;;; (equal? val v) holds.
(define tally-value
(lambda (lst val)
(tally lst (lambda (v) (equal? v val)))))
;;; Procedure:
;;; range
;;; Parameters:
;;; n, an integer
;;; Purpose:
;;; Constructs a list containing 0, ..., n-1 if n >= 0 and the empty
;;; list otherwise.
;;; Produces:
;;; result, a list
;;; Preconditions:
;;; [No additional]
;;; Postconditions:
;;; The length of result is n if n >= 0 and 0 otherwise.
(define range
(lambda (n)
(letrec ([reverse-helper
(lambda (acc l)
(cond [(null? l) acc]
[else (reverse-helper (cons (car l) acc)
(cdr l))]))]
[range-helper
(lambda (n)
(cond [(< n 0) '()]
[else (cons n (range-helper (- n 1)))]))])
(reverse-helper '() (range-helper (- n 1))))))
;;; Procedure:
;;; range1
;;; Parameters:
;;; n, an integer
;;; Purpose:
;;; Constructs a list containing 1, ..., n if n >= 0 and the empty
;;; list otherwise.
;;; Produces:
;;; result, a list
;;; Preconditions:
;;; [No additional]
;;; Postconditions:
;;; The length of result is n if n >= 0 and 0 otherwise.
(define range1
(lambda (n)
(map (lambda (n) (+ n 1)) (range n))))