-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlist-ext.satyg
549 lines (438 loc) · 12.9 KB
/
list-ext.satyg
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
@stage: persistent
@require: list
@import: ord
@import: eq
@import: option-ext
@import: ord
module List : sig
val eq : ('a Eq.t) implicit -> (('a list) Eq.t) implicit
val null : 'a list -> bool
val nil : 'a list
val cons : 'a -> 'a list -> 'a list
val uncons : 'a list -> ('a * 'a list) option
val map : ('a -> 'b) -> 'a list -> 'b list
val mapi : (int -> 'a -> 'b) -> 'a list -> 'b list
val iter : ('a -> unit) -> 'a list -> unit
val iteri : (int -> 'a -> unit) -> 'a list -> unit
val filter : ('a -> bool) -> 'a list -> 'a list
val reverse : 'a list -> 'a list
val append : 'a list -> 'a list -> 'a list
val concat : ('a list) list -> 'a list
val length : 'a list -> int
val nth : int -> 'a list -> 'a option
val intersperse : 'a -> 'a list -> 'a list
val find : ('a -> bool) -> 'a list -> 'a option
val apply : 'a -> ('a -> 'b) list -> 'b list
val take : int -> 'a list -> 'a list
val drop : int -> 'a list -> 'a list
val take-while : ('a -> bool) -> 'a list -> 'a list
val drop-while : ('a -> bool) -> 'a list -> 'a list
val split-at : int -> 'a list -> ('a list * 'a list)
val span : ('a -> bool) -> 'a list -> ('a list * 'a list)
val break : ('a -> bool) -> 'a list -> ('a list * 'a list)
val head : 'a list -> 'a option
val tail : 'a list -> ('a list) option
val last : 'a list -> 'a option
val init : 'a list -> ('a list) option
val reverse-append : 'a list -> 'a list -> 'a list
val reverse-map : ('a -> 'b) -> 'a list -> 'b list
val all : ('a -> bool) -> 'a list -> bool
val any : ('a -> bool) -> 'a list -> bool
val zip : 'a list -> 'b list -> ('a * 'b) list
val unzip : ('a * 'b) list -> 'a list * 'b list
val find : ('a -> bool) -> 'a list -> 'a option
val partition : ('a -> bool) -> 'a list -> 'a list * 'a list
val iterate : int -> ('a -> 'a) -> 'a -> 'a list
val repeat : int -> 'a -> 'a list
val cycle- : int -> 'a list -> 'a list
val acons : 'a -> 'b -> ('a * 'b) list -> ('a * 'b) list
val max : ('a Ord.t) implicit -> 'a list -> 'a option
val min : ('a Ord.t) implicit -> 'a list -> 'a option
val fold : 'b -> ('a -> 'b -> 'b) -> 'a list -> 'b
% unstable API (may subject to change in the future)
val fold-left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a
val fold-lefti : (int -> 'a -> 'b -> 'a) -> 'a -> 'b list -> 'a
val fold-right : ('a -> 'b -> 'b) -> 'b -> 'a list -> 'b
val fold-left-adjacent : ('a -> 'b -> 'b option -> 'b option -> 'a) -> 'a -> 'b list -> 'a
val map-adjacent : ('a -> 'a option -> 'a option -> 'b) -> 'a list -> 'b list
val mapi-adjacent : (int -> 'a -> 'a option -> 'a option -> 'b) -> 'a list -> 'b list
val bubblesort : ('a Ord.t) implicit -> 'a list -> 'a list
val insertsort : ('a Ord.t) implicit -> 'a list -> 'a list
val mergesort : ('a Ord.t) implicit -> 'a list -> 'a list
% deprecated
val assoc : ('a -> 'a -> bool) -> 'a -> ('a * 'b) list -> 'b option % TODO: use Eq
val takewhile : ('a -> bool) -> 'a list -> 'a list
val dropwhile : ('a -> bool) -> 'a list -> 'a list
val splitat : int -> 'a list -> ('a list * 'a list)
val all-and : bool list -> bool
val or : bool list -> bool
val for-all : ('a -> bool) -> 'a list -> bool
val exists : ('a -> bool) -> 'a list -> bool
val for-all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool option
val exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool option
val split : ('a * 'b) list -> 'a list * 'b list
val combine : 'a list -> 'b list -> (('a * 'b) list) option
val show-opt : ('a option) list -> 'a list
val make-cycle : int -> 'a list -> 'a list
end = struct
let nil = []
let cons x xs = x :: xs
let-rec map
| f [] = []
| f (x :: xs) = (f x) :: map f xs
let mapi f =
let-rec aux
| i f [] = []
| i f (x :: xs) = (f i x) :: aux (i + 1) f xs
in
aux 0 f
let-rec iter
| f [] = ()
| f (x :: xs) = f x before iter f xs
let iteri f =
let-rec aux
| i f [] = ()
| i f (x :: xs) = f i x before aux (i + 1) f xs
in
aux 0 f
let-rec fold-left
| f init [] = init
| f init (x :: xs) = fold-left f (f init x) xs
let fold-lefti f =
let-rec aux
| i f init [] = init
| i f init (x :: xs) = aux (i + 1) f (f i init x) xs
in
aux 0 f
let-rec fold-right
| f init [] = init
| f init (x :: xs) = f x (fold-right f init xs)
let-rec filter
| _ [] = []
| p (x :: xs) = if p x then x :: filter p xs else filter p xs
let-rec assoc
| _ _ [] = None
| eq a ((x, y) :: xs) = if eq a x then Some(y) else assoc eq a xs
let reverse lst =
fold-left (fun acc x -> x :: acc) [] lst
let-rec append lst1 lst2 =
match lst1 with
| [] -> lst2
| x :: xs -> x :: append xs lst2
let concat lst = fold-right append [] lst
let fold-left-adjacent f =
let-rec aux leftopt init lst =
match lst with
| [] ->
init
| head :: [] ->
let initnew = f init head leftopt None in
initnew
| head :: ((right :: _) as tail) ->
let initnew = f init head leftopt (Some(right)) in
aux (Some(head)) initnew tail
in
aux None
let map-adjacent f lst =
lst |> fold-left-adjacent (fun acc x leftopt rightopt -> (
f x leftopt rightopt :: acc
)) [] |> reverse
let mapi-adjacent f lst =
let (_, acc) =
lst |> fold-left-adjacent (fun (i, acc) x leftopt rightopt -> (
(i + 1, f i x leftopt rightopt :: acc)
)) (0, [])
in
reverse acc
let length lst =
fold-right (fun _ i -> i + 1) 0 lst
let nth lst =
let-rec aux i n xs =
match xs with
| [] -> None
| head :: tail -> if n == i then Some(head) else aux (i + 1) n tail
in
aux 0 lst
let eq eq =
let-rec aux
| [] [] = true
| (x::xs) (y::ys) = (Eq.equal eq x y) &&&- (fun () -> aux xs ys)
| _ _ = false
in Eq.make aux
let intersperse s xs =
match xs with
| [] -> []
| (x :: xs) ->
x :: List.concat (xs |> List.map (fun x -> [s; x]))
let-rec apply v lst =
match lst with
| [] -> []
| x :: xs -> x v :: apply v xs
let null lst =
match lst with
| [] -> true
| _ -> false
let-rec take i lst =
match lst with
| [] -> []
| x :: xs -> (
if i <= 0 then
[]
else
x :: (take (i - 1) xs)
)
let-rec drop i lst =
match lst with
| [] -> []
| _ :: xs -> (
if i <= 0 then
lst
else
drop (i - 1) xs
)
let-rec take-while f lst =
match lst with
| [] -> []
| x :: xs -> (
if f x then
x :: take-while f xs
else
take-while f xs
)
let takewhile f lst = take-while f lst
let-rec drop-while f lst =
match lst with
| [] -> []
| x :: xs -> (
if f x then
drop-while f xs
else
x :: drop-while f xs
)
let dropwhile f lst = drop-while f lst
let split-at n lst =
(take n lst, drop n lst)
let splitat n lst = split-at n lst
let span f lst =
let-rec span-sub lst1 lst2 =
match lst2 with
| y :: ys -> (
if f y then
span-sub (y :: lst1) ys
else
(List.reverse lst1, lst2)
)
| _ -> (List.reverse lst1, lst2)
in
span-sub [] lst
let break f lst =
let-rec break-sub lst1 lst2 =
match lst2 with
| y :: ys -> (
if f y then
(List.reverse lst1, lst2)
else
break-sub (y :: lst1) ys
)
| [] -> (List.reverse lst1, lst2)
in
break-sub [] lst
let head lst =
match lst with
| [] -> None
| head :: _ -> Some(head)
let tail lst =
match lst with
| [] -> None
| _ :: tail -> Some(tail)
let-rec last lst =
match lst with
| [] -> None
| x :: [] -> Some(x)
| _ :: xs -> last xs
let-rec init lst =
match lst with
| [] -> None
| x :: [] -> Some([])
| x :: xs -> Option.(Some(x) ^:: (init xs))
let-rec reverse-append lst1 lst2 =
match lst1 with
| [] -> lst2
| x :: xs -> reverse-append xs (x :: lst2)
let reverse-map f lst =
let-rec rmap-f accu lst =
match lst with
| [] -> accu
| (x :: xs) -> rmap-f (f x :: accu) xs
in
rmap-f [] lst
let-rec all-and lst =
match lst with
| [] -> true
| x :: xs -> x && all-and xs
let-rec or lst =
match lst with
| [] -> false
| x :: xs -> x || or xs
let-rec all f lst =
match lst with
| [] -> true
| x :: xs -> f x && all f xs
let for-all f lst = all f lst
let-rec any f lst =
match lst with
| [] -> false
| x :: xs -> f x || any f xs
let exists f lst = any f lst
let-rec for-all2 f lst1 lst2 =
match (lst1, lst2) with
| ([], []) -> Some(true)
| (x1 :: xs1, x2 :: xs2) -> Option.(Some(f x1 x2) ^&& for-all2 f xs1 xs2)
| (_, _) -> None
let-rec exists2 f lst1 lst2 =
match (lst1, lst2) with
| ([], []) -> Some(false)
| (x1 :: xs1, x2 :: xs2) -> Option.(Some(f x1 x2) ^|| (exists2 f xs1 xs2))
| (_, _) -> None
let-rec find f lst =
match lst with
| [] -> None
| x :: xs -> if f x then Some(x) else find f xs
let partition f lst =
let-rec part yes no lst =
match lst with
| [] -> (List.reverse yes, List.reverse no)
| x :: xs -> if f x then part (x :: yes) no xs else part yes (x :: no) xs
in
part [] [] lst
let-rec unzip lst =
match lst with
| [] -> ([], [])
| (x, y) :: xs -> let (rx, ry) = unzip xs in (x :: rx, y :: ry)
let split lst = unzip lst
let-rec zip lst1 lst2 =
match (lst1, lst2) with
| (x1 :: xs1, x2 :: xs2) -> (x1, x2) :: (zip xs1 xs2)
| (_, _) -> []
let-rec combine lst1 lst2 =
match (lst1, lst2) with
| ([], []) -> Some([])
| (x1 :: xs1, x2 :: xs2) -> Option.(Some((x1, x2)) ^:: (combine xs1 xs2))
| (_, _) -> None
let-rec show-opt lst =
match lst with
| [] -> []
| x :: xs -> (
match x with
| Some(x) -> x :: show-opt xs
| None -> show-opt xs
)
let-rec iterate n f initial =
if n == 0 then
[]
else
f initial :: iterate (n - 1) f (f initial)
let-rec repeat n initial =
if n == 0 then
[]
else
initial :: repeat (n - 1) initial
let-rec cycle- n lst =
if n == 0 then
[]
else
List.append lst (cycle- (n - 1) lst)
let make-cycle n lst = cycle- n lst
let-rec max ord lst =
match lst with
| [] -> None
| x :: [] -> Some(x)
| x :: y :: zs -> (
if Ord.lt ord x y then
max ord (y :: zs)
else
max ord (x :: zs)
)
let-rec min ord lst =
match lst with
| [] -> None
| x :: [] -> Some(x)
| x :: y :: zs -> (
if Ord.gt ord x y then
min ord (y :: zs)
else
min ord (x :: zs)
)
let-rec bubblesort ord lst =
let-rec last lst =
match lst with
| [] -> panic `List.bubblesort: empty list`
| x :: [] -> x
| _ :: xs -> last xs
in
let-rec init lst =
match lst with
| [] -> panic `List.bubblesort: empty list`
| _ :: [] -> []
| x :: xs -> x :: (init xs)
in
let-rec bs-sub lst =
match lst with
| (x :: y :: zs) -> (
if Ord.le ord x y then
y :: bs-sub (x :: zs)
else
x :: bs-sub (y :: zs)
)
| _ -> lst
in
match lst with
| [] -> []
| x :: [] -> [x]
| _ -> (
let a = bs-sub lst in
let x = last a in
let xs = init a in
match xs with
| [] -> x :: []
| _ -> x :: (bubblesort ord xs)
)
let-rec insertsort ord lst =
let-rec is-sub x lst =
match lst with
| [] -> [x]
| y :: ys -> (
if Ord.le ord x y then
x :: y :: ys
else
y :: is-sub x (ys)
)
in
match lst with
| [] -> []
| x :: xs -> is-sub x (insertsort ord xs)
let-rec mergesort ord lst =
let-rec merge lst1 lst2 =
match (lst1, lst2) with
| (lst1, []) -> lst1
| ([], lst2) -> lst2
| ((x :: xs), (y :: ys)) ->
if Ord.le ord x y then
x :: (merge xs lst2)
else
y :: (merge lst1 ys)
in
match lst with
| [] -> []
| [x] -> [x]
| _ ->
let (ys, zs) = splitat ((length lst) / 2) lst in
merge (mergesort ord ys) (mergesort ord zs)
let acons k v alist = (k, v) :: alist
let uncons xs =
match xs with
| [] -> None
| x :: xs -> Some(x, xs)
let fold x f l =
fold-left (Fn.flip f) x l
end