-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfull_stl.txt
263 lines (211 loc) · 4.5 KB
/
full_stl.txt
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
(def len (lst)
(if (eq lst []),
0,
(succ (len (cdr lst)))
)
)
(def sum (lst)
(if (eq lst []) 0
(add (car lst)
(sum (cdr lst))
)
)
)
(def sum_t (lst acc)
(if (eq lst []) acc (sum_t (cdr lst) (add (car lst ) acc)))
)
(def map (fn lst)
(if (eq lst []) []
(cons (fn (car lst))
(map fn (cdr lst))
)
)
)
(def nil? (lst) (eq lst []))
(def fold (f acc lst)
(if (nil? lst) acc
(fold f
(f acc (car lst))
(cdr lst)
)
)
)
(def not (bool) (if (eq bool false) true false))
(def take (n lst)
(if (or (nil? lst) (eq n 0)) []
(cons (car lst)
(take (pred n) (cdr lst))
)
)
)
(def drop (n lst)
(if (nil? lst) []
(if (eq n 0) lst
(drop (pred n) (cdr lst))
)
)
)
(def iter (f x)
(cons x (iter f (f x)))
)
(def id (x) x)
(def times (f x n)
(if (eq n 0) x
(f (times f x (pred n)))
)
)
(def ltake (n lst)
(if (eq n 0) []
(cons (lcar lst) (ltake (pred n) (lcdr lst)))
)
)
# f: unary, x starting elem #
(def fcons (f x)
(lcons x (fcons f (f x)))
)
(def lmap (f lst)
(lcons (f (lcar lst))
(lmap f (lcdr lst))
)
)
(def ldrop (n lst)
(times lcdr lst n)
)
(def lfold (f acc lst)
(let new (f acc (lcar lst))
(lcons new
(lfold f new (lcdr lst))
)
)
)
(def fibs (fst snd)
(let sum (add fst snd) (lcons snd (fibs snd sum)))
)
(def lfilter (pred lst)
(let fst (lcar lst) (if (pred fst) (lcons fst (lfilter pred (lcdr lst)))
(lfilter pred (lcdr lst))
))
)
(def even (n) (eq (mod n 2) 0))
(def odd (n) (not (even n)))
(set nats (fcons succ 1))
(def rev (lst)
(if (nil? (cdr lst)) lst (cons (rev (cdr lst)) [(car lst)]))
)
(def filter (pred lst)
(if (nil? lst) []
(if (pred (car lst)) (cons (car lst) (filter pred (cdr lst)))
(filter pred (cdr lst))
)
)
)
# filter gt,lt and qsort on those then cons#
(def app (arr x)
(cons arr [x])
)
# x>=y #
(def gte (x y)
(eq (or (eq x y) (gt x y)) true)
)
(def nilone? (lst)
(lt (len lst) 2)
)
(def qsort (lst)
(if (lt (len lst) 2) (if (nil? lst) [] lst)
(let length (len lst) pivot (idx lst (rand 0 (sub length 1))) left_arr (filter (gt pivot) lst) right_arr (filter (lt pivot) lst)
(cons (app (qsort left_arr) pivot) (qsort right_arr))
)
)
)
(def merge (lst1 lst2)
(if (nil? lst1) lst2 (if (nil? lst2) lst1
(
let fst (idx lst1 0),
snd (idx lst2 0),
check (lt fst snd)
hd (if check fst snd),
smaller (if check lst1 lst2),
bigger (if check lst2 lst1),
(if (eq lst1 smaller) (cons hd (merge (cdr lst1) lst2))
(cons hd (merge lst1 (cdr lst2)))
)
)
)
)
)
(def msort (lst)
(if (nilone? lst) lst
(let mid (div (len lst) 2) left (take mid lst) right (drop mid lst)
(merge (msort left) (msort right))
)
)
)
(def apply (f1 f2 elem) (eq (f1 elem) (f2 elem)))
(def neq (x y)
(not (eq x y))
)
(def prime (n)
(and (neq n 1)
(nilone?
(filter (z->(eq (mod n z) 0))
(takeWhile (gt n) (fcons succ 1))
)
)
)
)
(def primes (n)
(lfilter prime (fcons succ 2))
)
(def recr (n) (if (eq n 0) 0 (add n (recr (pred n)))))
(def recf (n acc)
(if (eq n 0) acc (recf (pred n) (add acc n)));
)
(def recr_t (n acc) (if (eq n 0) acc (recr_t (pred n) (add acc n))))
(def takeWhile (pred lst)
(if (and (not $ (nil? lst)) (pred (car lst)))
(cons (car lst)
(takeWhile pred (cdr lst)),
),
[]
)
)
(def range (start end)
(
(if (gt start end) []
(lcons start
(range (succ start) end)
)
)
)
)
(def for_each (lst f)
(if (nil? lst)
[],
(
(> (f (car lst)) (for_each (cdr lst) f))
)
)
)
# end = length #
(def loop_idx (lst st end)
(if (gte st end) []
(> (puts (idx lst st)) (loop_idx lst (succ st) end))
)
)
#(def nested (lst)
(for_each (range 1 (len lst))
(def fn (i)
(for_each (range 0 i) (i-> (idx lst i)))
)
)
)#
(def idx (i lst)
(if (eq i 0) (car lst)
(idx (pred i) (cdr lst))
)
)
(def nested (lst)
(for_each (range 0 (len lst))
(i-> (idx i lst))
)
)