-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples-488.rkt
240 lines (190 loc) · 5.23 KB
/
examples-488.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
239
240
#lang racket
'(begin
(define true
(λ (x)
(λ (y) (x 666))))
(define false
(λ (x)
(λ (y) (y 666))))
(define empty
(λ (msg)
true))
(define (empty? b)
(b true))
(define (box val)
(λ (msg)
(if msg
false
val)))
(define (unbox b)
(b false))
(define (pair a b)
(λ (msg)
(if msg a b)))
(define (aft p)
(p false))
(define (fore p)
(p true))
(define (cons e ls)
(box (pair e ls)))
(define (first ls)
(fore (unbox ls)))
(define (rest ls)
(aft (unbox ls)))
(define (compose f g)
(λ (x) (f (g x))))
(define (identity x) x)
(define second
(compose first rest))
(define third
(compose second rest))
; right now everything is autocurried
#; (define (curry f a)
(λ (x) (f a x)))
(define (fold f init ls)
(if (empty? ls)
init
(f (first ls)
(fold f
init
(rest ls)))))
(define (append a b)
(fold cons b a))
(define (map f ls)
(fold (λ (x ys)
(cons (f x) ys))
empty ls))
(define (filter f ls)
(fold (λ (x ys)
(if (f x)
(cons x ys)
ys))
empty ls))
; introducing zero, increment
(define (length ls)
(fold (λ (a bs)
(+ 1 bs))
0 ls))
; todo: try implementing foldl with foldr
(define (foldl f accum ls)
(if (empty? ls)
accum
(foldl f
(f (first ls) accum)
(rest ls))))
(define (reverse ls)
(foldl cons
empty ls))
(define last
(compose first
reverse))
; introducing decrement
(define (list-ref ls i)
(if (< i 1)
(first ls)
(list-ref (rest ls)
(+ i -1))))
(define (not a)
(if a
false
true))
(define (or a b)
(if a
true
(if b
true
false)))
(define (and a b)
(if a
(if b
true
false)
false))
(define (>= a b)
(not (< a b)))
; equal? for integers
(define (=? a b)
(and (>= a b)
(< a (+ 1 b))))
(define (<= a b)
(or (< a b)
(=? a b)))
(define (> a b)
(not (<= a b)))
; not very efficient (append, double filtering)
(define (quicksort ls)
(if (< (length ls) 2)
ls
(begin
(define pivot
(first ls))
(define under
(filter (λ (x)
(< x pivot))
ls))
(define over
(rest (filter (λ (x)
(>= x pivot))
ls)))
(append (quicksort under)
(append (cons pivot empty)
(quicksort over))))))
; todo:
#;(define (list-equal? a b)); needs types?
#;(define member? 0); needs equality
#;(define (assoc v ls)); needs equality
; numerical functions: add1 sub1 - %
; boolean forms in R* (short circuiting)
; boolean combinators: negate conjoin disjoin
; test tests (yo dawg)
(define list-A
(cons 11
(cons 22
(cons 33
(cons 44
empty)))))
(define list-B
(cons 55
(cons 66
empty)))
(define list-unsorted
(cons 55
(cons 22
(cons 44
(cons 11
(cons 33 empty))))))
; this will output the numeric label of the first
; failing test, or 0 if all tests succeed
(check =?
((1 (first list-A) 11)
(2 (first (rest list-A)) 22)
(3 (second list-A) 22)
(4 (third list-A) 33)
(5 (first (reverse list-A)) 44)
(6 (last list-A) 44)
(7 (length list-A) 4)
(8 (second (map (λ (x) (+ x 4)) list-A)) 26)
(9 (third (reverse (append list-A list-B))) 44)
(10 (list-ref list-A 2) 33)
(11 (length (filter (λ (x) (> x 22)) list-unsorted)) 3)
(12 (first (filter (λ (x) (> 22 x)) list-unsorted)) 11)
(13 (if (empty? empty) 1 0) 1)
(14 (if (empty? (cons 1 empty)) 1 0) 0)
; these three don't actually test much...
(15 (if (=? 3 3) 1 0) 1)
(16 (if (=? 4 3) 1 0) 0)
(17 (if (=? 3 4) 1 0) 0)
(18 (if (>= 4 4) 1 0) 1)
(19 (if (>= 4 3) 1 0) 1)
(20 (if (>= 3 4) 1 0) 0)
(21 (if (<= 4 4) 1 0) 1)
(22 (if (<= 4 3) 1 0) 0)
(23 (if (<= 3 4) 1 0) 1)
(24 (if (> 4 4) 1 0) 0)
(25 (if (> 4 3) 1 0) 1)
(26 (if (> 3 4) 1 0) 0)
(27 (first (quicksort list-unsorted)) 11)
(28 (second (quicksort list-unsorted)) 22)
(29 (third (quicksort list-unsorted)) 33)
(666 0 1)))
)