-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslist.ml
44 lines (35 loc) · 901 Bytes
/
slist.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
(* Tail-recursive lists *)
module List :
sig
include (module type of List)
end
=
struct
include List
let map f l =
List.rev (List.rev_map f l)
let append a b =
List.rev_append (List.rev a) b
let ( @ ) = append
let map f l =
List.rev (List.rev_map f l)
let rev_map2 f l l2 =
let rec rev_map_inner acc a b =
match a, b with
[], [] -> acc
| x::xs, y::ys -> rev_map_inner (f x y :: acc) xs ys
| _ -> raise (Invalid_argument "List.map2")
in
rev_map_inner [] l l2
let map2 f l l2 =
List.rev (rev_map2 f l l2)
let concat lists =
let rec concat out acc =
match acc with
| [] -> out
| l::ls -> concat (append l out) ls
in
concat [] (List.rev lists)
let fold_right f l e =
List.fold_left (fun x y -> f y x) e (List.rev l)
end