-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ml
54 lines (44 loc) · 1.46 KB
/
util.ml
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
let rec zip l1 l2 =
match (l1, l2) with
| h1 :: l1, h2 :: l2 -> (h1, h2) :: zip l1 l2
| [], [] -> []
| _ -> raise @@ Invalid_argument "lists that zip takes must be same length"
let rec zip3 l1 l2 l3 =
match (l1, l2, l3) with
| h1 :: l1, h2 :: l2, h3 :: l3 -> (h1, h2, h3) :: zip3 l1 l2 l3
| [], [], [] -> []
| _ ->
raise @@ Invalid_argument "lists that zip3 takes must be same length"
let rec zip4 l1 l2 l3 l4 =
match (l1, l2, l3, l4) with
| h1 :: l1, h2 :: l2, h3 :: l3, h4 :: l4 ->
(h1, h2, h3, h4) :: zip4 l1 l2 l3 l4
| [], [], [], [] -> []
| _ ->
raise @@ Invalid_argument "lists that zip3 takes must be same length"
let rec unzip = function
| [] -> ([], [])
| (a, b) :: remain ->
let a', b' = unzip remain in
(a :: a', b :: b')
let rec unzip3 = function
| [] -> ([], [], [])
| (a, b, c) :: remain ->
let a', b', c' = unzip3 remain in
(a :: a', b :: b', c :: c')
let rec unzip4 = function
| [] -> ([], [], [], [])
| (a, b, c, d) :: remain ->
let a', b', c', d' = unzip4 remain in
(a :: a', b :: b', c :: c', d :: d')
let rec drop n l =
match (n, l) with
| 0, l -> l
| n, h :: l -> drop (n - 1) l
| _ -> raise @@ Invalid_argument "cannot drop"
let rec uniq = function
| x :: remain -> x :: (uniq @@ List.filter (( <> ) x) remain)
| [] -> []
let fst (x, _, _) = x
let snd (_, x, _) = x
let trd (_, _, x) = x