summaryrefslogtreecommitdiff
path: root/src/vylist.ml
blob: c64dd8942522b8fb2959bab9b5826b9be6d19c14 (plain)
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
let rec find p xs =
    match xs with
    | [] -> None
    | y :: ys -> if (p y) then (Some y)
                 else find p ys

let rec remove p xs =
    match xs with
    | [] -> []
    | y :: ys -> if (p y) then ys
                 else y :: (remove p ys)

let rec replace p x xs =
    match xs with
    | [] -> raise Not_found
    | y :: ys -> if (p y) then x :: ys
                 else y :: (replace p x ys)

let rec insert_before p x xs =
    match xs with
    | [] -> raise Not_found
    | y :: ys -> if (p y) then x :: y :: ys
                 else y :: (insert_before p x ys)

let rec insert_after p x xs =
    match xs with
    | [] -> raise Not_found
    | y :: ys -> if (p y) then y :: x :: ys
                 else y :: (insert_after p x ys)

let rec insert_compare p x xs =
    match xs with
    | [] -> [x]
    | y :: ys -> if (p x y <= 0) then x :: y :: ys
                 else y :: (insert_compare p x ys)

let complement xs ys =
    let rec aux xs ys =
        match xs, ys with
        | [], _ -> ys
        | _, [] -> assert false (* Can't happen *)
        | p :: ps, q :: qs -> if p = q then aux ps qs
                              else []
    in
    if List.length xs < List.length ys then aux xs ys
    else aux ys xs

let in_list xs x =
    let x' = find ((=) x) xs in
    match x' with
    | None -> false
    | Some _ -> true