4 | Cons of ('a * 'a listx)
6 let rec fold_left f acc l =
9 | Cons (x, l') -> fold_left f (f acc x) l'
19 | Cons (x, l') -> Cons (f x, map f l')
23 | Nil x -> Cons (x, l)
24 | Cons (x, l') -> Cons (x, append l l')
26 let rec length = function
28 | Cons (_, xs) -> 1 + (length xs)
30 let rec assoc x = function
32 | Cons ((y,t),_) when x=y -> t
33 | Nil _ -> raise Not_found
34 | Cons (_,l) -> assoc x l
39 | Cons (x,l) -> x::to_list l
43 [] -> raise (Failure "from_list: empty list")
45 | x::l -> Cons(x,from_list l)
47 let rec split_nth n l =
51 | n,Cons (hd,tl) -> hd::split_nth (n-1) tl
52 | _,_ -> raise (Failure "split_nth: not enough args")
57 | Cons (x, l) -> Pervasives.max x (max l)
62 | Cons (_, l) -> last l
64 let rec nth i l = match l, i with
66 | _, n when n <= 0 -> raise (Invalid_argument "Listx.nth")
67 | Cons (_, xs), n -> nth (n-1) xs
69 | Nil _, _ -> raise (Invalid_argument "Listx.nth")