-
Notifications
You must be signed in to change notification settings - Fork 460
/
Copy pathlist.res
749 lines (674 loc) · 15.6 KB
/
list.res
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/* ************************************************************************ */
/* */
/* OCaml */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/* ************************************************************************ */
/* List operations */
let rec length_aux = (len, param) =>
switch param {
| list{} => len
| list{_, ...l} => length_aux(len + 1, l)
}
let length = l => length_aux(0, l)
let cons = (a, l) => list{a, ...l}
let hd = param =>
switch param {
| list{} => failwith("hd")
| list{a, ..._} => a
}
let tl = param =>
switch param {
| list{} => failwith("tl")
| list{_, ...l} => l
}
let nth = (l, n) =>
if n < 0 {
invalid_arg("List.nth")
} else {
let rec nth_aux = (l, n) =>
switch l {
| list{} => failwith("nth")
| list{a, ...l} =>
if n == 0 {
a
} else {
nth_aux(l, n - 1)
}
}
nth_aux(l, n)
}
let nth_opt = (l, n) =>
if n < 0 {
invalid_arg("List.nth")
} else {
let rec nth_aux = (l, n) =>
switch l {
| list{} => None
| list{a, ...l} =>
if n == 0 {
Some(a)
} else {
nth_aux(l, n - 1)
}
}
nth_aux(l, n)
}
let append = \"@"
let rec rev_append = (l1, l2) =>
switch l1 {
| list{} => l2
| list{a, ...l} => rev_append(l, list{a, ...l2})
}
let rev = l => rev_append(l, list{})
let rec init_tailrec_aux = (acc, i, n, f) =>
if i >= n {
acc
} else {
init_tailrec_aux(list{f(i), ...acc}, i + 1, n, f)
}
let rec init_aux = (i, n, f) =>
if i >= n {
list{}
} else {
let r = f(i)
list{r, ...init_aux(i + 1, n, f)}
}
let init = (len, f) =>
if len < 0 {
invalid_arg("List.init")
} else if len > 10_000 {
rev(init_tailrec_aux(list{}, 0, len, f))
} else {
init_aux(0, len, f)
}
let rec flatten = param =>
switch param {
| list{} => list{}
| list{l, ...r} => \"@"(l, flatten(r))
}
let concat = flatten
let rec map = (f, param) =>
switch param {
| list{} => list{}
| list{a, ...l} =>
let r = f(a)
list{r, ...map(f, l)}
}
let rec mapi = (i, f, param) =>
switch param {
| list{} => list{}
| list{a, ...l} =>
let r = f(i, a)
list{r, ...mapi(i + 1, f, l)}
}
let mapi = (f, l) => mapi(0, f, l)
let rev_map = (f, l) => {
let rec rmap_f = (accu, param) =>
switch param {
| list{} => accu
| list{a, ...l} => rmap_f(list{f(a), ...accu}, l)
}
rmap_f(list{}, l)
}
let rec iter = (f, param) =>
switch param {
| list{} => ()
| list{a, ...l} =>
f(a)
iter(f, l)
}
let rec iteri = (i, f, param) =>
switch param {
| list{} => ()
| list{a, ...l} =>
f(i, a)
iteri(i + 1, f, l)
}
let iteri = (f, l) => iteri(0, f, l)
let rec fold_left = (f, accu, l) =>
switch l {
| list{} => accu
| list{a, ...l} => fold_left(f, f(accu, a), l)
}
let rec fold_right = (f, l, accu) =>
switch l {
| list{} => accu
| list{a, ...l} => f(a, fold_right(f, l, accu))
}
let rec map2 = (f, l1, l2) =>
switch (l1, l2) {
| (list{}, list{}) => list{}
| (list{a1, ...l1}, list{a2, ...l2}) =>
let r = f(a1, a2)
list{r, ...map2(f, l1, l2)}
| (_, _) => invalid_arg("List.map2")
}
let rev_map2 = (f, l1, l2) => {
let rec rmap2_f = (accu, l1, l2) =>
switch (l1, l2) {
| (list{}, list{}) => accu
| (list{a1, ...l1}, list{a2, ...l2}) => rmap2_f(list{f(a1, a2), ...accu}, l1, l2)
| (_, _) => invalid_arg("List.rev_map2")
}
rmap2_f(list{}, l1, l2)
}
let rec iter2 = (f, l1, l2) =>
switch (l1, l2) {
| (list{}, list{}) => ()
| (list{a1, ...l1}, list{a2, ...l2}) =>
f(a1, a2)
iter2(f, l1, l2)
| (_, _) => invalid_arg("List.iter2")
}
let rec fold_left2 = (f, accu, l1, l2) =>
switch (l1, l2) {
| (list{}, list{}) => accu
| (list{a1, ...l1}, list{a2, ...l2}) => fold_left2(f, f(accu, a1, a2), l1, l2)
| (_, _) => invalid_arg("List.fold_left2")
}
let rec fold_right2 = (f, l1, l2, accu) =>
switch (l1, l2) {
| (list{}, list{}) => accu
| (list{a1, ...l1}, list{a2, ...l2}) => f(a1, a2, fold_right2(f, l1, l2, accu))
| (_, _) => invalid_arg("List.fold_right2")
}
let rec for_all = (p, param) =>
switch param {
| list{} => true
| list{a, ...l} => p(a) && for_all(p, l)
}
let rec exists = (p, param) =>
switch param {
| list{} => false
| list{a, ...l} => p(a) || exists(p, l)
}
let rec for_all2 = (p, l1, l2) =>
switch (l1, l2) {
| (list{}, list{}) => true
| (list{a1, ...l1}, list{a2, ...l2}) => p(a1, a2) && for_all2(p, l1, l2)
| (_, _) => invalid_arg("List.for_all2")
}
let rec exists2 = (p, l1, l2) =>
switch (l1, l2) {
| (list{}, list{}) => false
| (list{a1, ...l1}, list{a2, ...l2}) => p(a1, a2) || exists2(p, l1, l2)
| (_, _) => invalid_arg("List.exists2")
}
let rec mem = (x, param) =>
switch param {
| list{} => false
| list{a, ...l} => compare(a, x) == 0 || mem(x, l)
}
let rec memq = (x, param) =>
switch param {
| list{} => false
| list{a, ...l} => a === x || memq(x, l)
}
let rec assoc = (x, param) =>
switch param {
| list{} => raise(Not_found)
| list{(a, b), ...l} =>
if compare(a, x) == 0 {
b
} else {
assoc(x, l)
}
}
let rec assoc_opt = (x, param) =>
switch param {
| list{} => None
| list{(a, b), ...l} =>
if compare(a, x) == 0 {
Some(b)
} else {
assoc_opt(x, l)
}
}
let rec assq = (x, param) =>
switch param {
| list{} => raise(Not_found)
| list{(a, b), ...l} =>
if a === x {
b
} else {
assq(x, l)
}
}
let rec assq_opt = (x, param) =>
switch param {
| list{} => None
| list{(a, b), ...l} =>
if a === x {
Some(b)
} else {
assq_opt(x, l)
}
}
let rec mem_assoc = (x, param) =>
switch param {
| list{} => false
| list{(a, _), ...l} => compare(a, x) == 0 || mem_assoc(x, l)
}
let rec mem_assq = (x, param) =>
switch param {
| list{} => false
| list{(a, _), ...l} => a === x || mem_assq(x, l)
}
let rec remove_assoc = (x, param) =>
switch param {
| list{} => list{}
| list{(a, _) as pair, ...l} =>
if compare(a, x) == 0 {
l
} else {
list{pair, ...remove_assoc(x, l)}
}
}
let rec remove_assq = (x, param) =>
switch param {
| list{} => list{}
| list{(a, _) as pair, ...l} =>
if a === x {
l
} else {
list{pair, ...remove_assq(x, l)}
}
}
let rec find = (p, param) =>
switch param {
| list{} => raise(Not_found)
| list{x, ...l} =>
if p(x) {
x
} else {
find(p, l)
}
}
let rec find_opt = (p, param) =>
switch param {
| list{} => None
| list{x, ...l} =>
if p(x) {
Some(x)
} else {
find_opt(p, l)
}
}
let find_all = (p, l) => {
let rec find = (accu, param) =>
switch param {
| list{} => rev(accu)
| list{x, ...l} =>
if p(x) {
find(list{x, ...accu}, l)
} else {
find(accu, l)
}
}
find(list{}, l)
}
let filter = find_all
let partition = (p, l) => {
let rec part = (yes, no, param) =>
switch param {
| list{} => (rev(yes), rev(no))
| list{x, ...l} =>
if p(x) {
part(list{x, ...yes}, no, l)
} else {
part(yes, list{x, ...no}, l)
}
}
part(list{}, list{}, l)
}
let rec split = param =>
switch param {
| list{} => (list{}, list{})
| list{(x, y), ...l} =>
let (rx, ry) = split(l)
(list{x, ...rx}, list{y, ...ry})
}
let rec combine = (l1, l2) =>
switch (l1, l2) {
| (list{}, list{}) => list{}
| (list{a1, ...l1}, list{a2, ...l2}) => list{(a1, a2), ...combine(l1, l2)}
| (_, _) => invalid_arg("List.combine")
}
/* sorting */
let rec merge = (cmp, l1, l2) =>
switch (l1, l2) {
| (list{}, l2) => l2
| (l1, list{}) => l1
| (list{h1, ...t1}, list{h2, ...t2}) =>
if cmp(h1, h2) <= 0 {
list{h1, ...merge(cmp, t1, l2)}
} else {
list{h2, ...merge(cmp, l1, t2)}
}
}
let rec chop = (k, l) =>
if k == 0 {
l
} else {
switch l {
| list{_, ...t} => chop(k - 1, t)
| _ => assert(false)
}
}
let stable_sort = (cmp, l) => {
let rec rev_merge = (l1, l2, accu) =>
switch (l1, l2) {
| (list{}, l2) => rev_append(l2, accu)
| (l1, list{}) => rev_append(l1, accu)
| (list{h1, ...t1}, list{h2, ...t2}) =>
if cmp(h1, h2) <= 0 {
rev_merge(t1, l2, list{h1, ...accu})
} else {
rev_merge(l1, t2, list{h2, ...accu})
}
}
let rec rev_merge_rev = (l1, l2, accu) =>
switch (l1, l2) {
| (list{}, l2) => rev_append(l2, accu)
| (l1, list{}) => rev_append(l1, accu)
| (list{h1, ...t1}, list{h2, ...t2}) =>
if cmp(h1, h2) > 0 {
rev_merge_rev(t1, l2, list{h1, ...accu})
} else {
rev_merge_rev(l1, t2, list{h2, ...accu})
}
}
let rec sort = (n, l) =>
switch (n, l) {
| (2, list{x1, x2, ..._}) =>
if cmp(x1, x2) <= 0 {
list{x1, x2}
} else {
list{x2, x1}
}
| (3, list{x1, x2, x3, ..._}) =>
if cmp(x1, x2) <= 0 {
if cmp(x2, x3) <= 0 {
list{x1, x2, x3}
} else if cmp(x1, x3) <= 0 {
list{x1, x3, x2}
} else {
list{x3, x1, x2}
}
} else if cmp(x1, x3) <= 0 {
list{x2, x1, x3}
} else if cmp(x2, x3) <= 0 {
list{x2, x3, x1}
} else {
list{x3, x2, x1}
}
| (n, l) =>
let n1 = asr(n, 1)
let n2 = n - n1
let l2 = chop(n1, l)
let s1 = rev_sort(n1, l)
let s2 = rev_sort(n2, l2)
rev_merge_rev(s1, s2, list{})
}
and rev_sort = (n, l) =>
switch (n, l) {
| (2, list{x1, x2, ..._}) =>
if cmp(x1, x2) > 0 {
list{x1, x2}
} else {
list{x2, x1}
}
| (3, list{x1, x2, x3, ..._}) =>
if cmp(x1, x2) > 0 {
if cmp(x2, x3) > 0 {
list{x1, x2, x3}
} else if cmp(x1, x3) > 0 {
list{x1, x3, x2}
} else {
list{x3, x1, x2}
}
} else if cmp(x1, x3) > 0 {
list{x2, x1, x3}
} else if cmp(x2, x3) > 0 {
list{x2, x3, x1}
} else {
list{x3, x2, x1}
}
| (n, l) =>
let n1 = asr(n, 1)
let n2 = n - n1
let l2 = chop(n1, l)
let s1 = sort(n1, l)
let s2 = sort(n2, l2)
rev_merge(s1, s2, list{})
}
let len = length(l)
if len < 2 {
l
} else {
sort(len, l)
}
}
let sort = stable_sort
let fast_sort = stable_sort
/* Note: on a list of length between about 100000 (depending on the minor
heap size and the type of the list) and Sys.max_array_size, it is
actually faster to use the following, but it might also use more memory
because the argument list cannot be deallocated incrementally.
Also, there seems to be a bug in this code or in the
implementation of obj_truncate.
external obj_truncate : 'a array -> int -> unit = "caml_obj_truncate"
let array_to_list_in_place a =
let l = Array.length a in
let rec loop accu n p =
if p <= 0 then accu else begin
if p = n then begin
obj_truncate a p;
loop (a.(p-1) :: accu) (n-1000) (p-1)
end else begin
loop (a.(p-1) :: accu) n (p-1)
end
end
in
loop [] (l-1000) l
let stable_sort cmp l =
let a = Array.of_list l in
Array.stable_sort cmp a;
array_to_list_in_place a
*/
/* sorting + removing duplicates */
let sort_uniq = (cmp, l) => {
let rec rev_merge = (l1, l2, accu) =>
switch (l1, l2) {
| (list{}, l2) => rev_append(l2, accu)
| (l1, list{}) => rev_append(l1, accu)
| (list{h1, ...t1}, list{h2, ...t2}) =>
let c = cmp(h1, h2)
if c == 0 {
rev_merge(t1, t2, list{h1, ...accu})
} else if c < 0 {
rev_merge(t1, l2, list{h1, ...accu})
} else {
rev_merge(l1, t2, list{h2, ...accu})
}
}
let rec rev_merge_rev = (l1, l2, accu) =>
switch (l1, l2) {
| (list{}, l2) => rev_append(l2, accu)
| (l1, list{}) => rev_append(l1, accu)
| (list{h1, ...t1}, list{h2, ...t2}) =>
let c = cmp(h1, h2)
if c == 0 {
rev_merge_rev(t1, t2, list{h1, ...accu})
} else if c > 0 {
rev_merge_rev(t1, l2, list{h1, ...accu})
} else {
rev_merge_rev(l1, t2, list{h2, ...accu})
}
}
let rec sort = (n, l) =>
switch (n, l) {
| (2, list{x1, x2, ..._}) =>
let c = cmp(x1, x2)
if c == 0 {
list{x1}
} else if c < 0 {
list{x1, x2}
} else {
list{x2, x1}
}
| (3, list{x1, x2, x3, ..._}) =>
let c = cmp(x1, x2)
if c == 0 {
let c = cmp(x2, x3)
if c == 0 {
list{x2}
} else if c < 0 {
list{x2, x3}
} else {
list{x3, x2}
}
} else if c < 0 {
let c = cmp(x2, x3)
if c == 0 {
list{x1, x2}
} else if c < 0 {
list{x1, x2, x3}
} else {
let c = cmp(x1, x3)
if c == 0 {
list{x1, x2}
} else if c < 0 {
list{x1, x3, x2}
} else {
list{x3, x1, x2}
}
}
} else {
let c = cmp(x1, x3)
if c == 0 {
list{x2, x1}
} else if c < 0 {
list{x2, x1, x3}
} else {
let c = cmp(x2, x3)
if c == 0 {
list{x2, x1}
} else if c < 0 {
list{x2, x3, x1}
} else {
list{x3, x2, x1}
}
}
}
| (n, l) =>
let n1 = asr(n, 1)
let n2 = n - n1
let l2 = chop(n1, l)
let s1 = rev_sort(n1, l)
let s2 = rev_sort(n2, l2)
rev_merge_rev(s1, s2, list{})
}
and rev_sort = (n, l) =>
switch (n, l) {
| (2, list{x1, x2, ..._}) =>
let c = cmp(x1, x2)
if c == 0 {
list{x1}
} else if c > 0 {
list{x1, x2}
} else {
list{x2, x1}
}
| (3, list{x1, x2, x3, ..._}) =>
let c = cmp(x1, x2)
if c == 0 {
let c = cmp(x2, x3)
if c == 0 {
list{x2}
} else if c > 0 {
list{x2, x3}
} else {
list{x3, x2}
}
} else if c > 0 {
let c = cmp(x2, x3)
if c == 0 {
list{x1, x2}
} else if c > 0 {
list{x1, x2, x3}
} else {
let c = cmp(x1, x3)
if c == 0 {
list{x1, x2}
} else if c > 0 {
list{x1, x3, x2}
} else {
list{x3, x1, x2}
}
}
} else {
let c = cmp(x1, x3)
if c == 0 {
list{x2, x1}
} else if c > 0 {
list{x2, x1, x3}
} else {
let c = cmp(x2, x3)
if c == 0 {
list{x2, x1}
} else if c > 0 {
list{x2, x3, x1}
} else {
list{x3, x2, x1}
}
}
}
| (n, l) =>
let n1 = asr(n, 1)
let n2 = n - n1
let l2 = chop(n1, l)
let s1 = sort(n1, l)
let s2 = sort(n2, l2)
rev_merge(s1, s2, list{})
}
let len = length(l)
if len < 2 {
l
} else {
sort(len, l)
}
}
let rec compare_lengths = (l1, l2) =>
switch (l1, l2) {
| (list{}, list{}) => 0
| (list{}, _) => -1
| (_, list{}) => 1
| (list{_, ...l1}, list{_, ...l2}) => compare_lengths(l1, l2)
}
let rec compare_length_with = (l, n) =>
switch l {
| list{} =>
if n == 0 {
0
} else if n > 0 {
-1
} else {
1
}
| list{_, ...l} =>
if n <= 0 {
1
} else {
compare_length_with(l, n - 1)
}
}