package minicaml

  1. Overview
  2. Docs

Source file 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
55
56
57
58
59
60
61
62
63
64
65
(** Helper function to take the first elements of a list *)
let rec take k xs = match k with
  | 0 -> []
  | k -> match xs with
  | [] -> failwith "take"
  | y::ys -> y :: (take (k - 1) ys)

(** Helper function to drop the first elements of a list *)
let rec drop k xs = match k with
  | 0 -> xs
  | k -> match xs with
  | [] -> failwith "drop"
  | _::ys -> (drop (k - 1) ys)

let fst (a, _) = a
let snd (_, a) = a

let fstl l = List.map fst l
let sndl l = List.map snd l

(** Helper function to unzip a list of couples *)
let unzip l = if l = [] then ([], []) else
  (fstl l, sndl l)

(* *)
let rec zip l1 l2 =
  match (l1, l2) with
  | ([], []) -> []
  | (x::xs, y::ys) -> (x,y)::(zip xs ys)
  | _ -> failwith "lists are not of equal length"

(* Search for duplicates *)
let rec dup_exist = function
  | [] -> false
  | hd::tl -> List.exists ((=) hd) tl || dup_exist tl

(* Search for duplicates in a list of key-value pairs *)
let rec dup_key_exist = function
  | [] -> false
  | (hk, _)::tl -> List.exists (fun (x,_) -> x = hk) tl || dup_key_exist tl

let rec delete_key ks l = match l with
  | [] -> []
  | (k, v)::xs -> if k = ks
  then delete_key ks xs
  else (k, v)::(delete_key ks xs)

(* Search for key in a list of key-value pairs *)
let rec key_exist ks l = match l with
  | [] -> false
  | (k, _)::xs -> if k = ks
  then true
  else key_exist ks xs

(* Search and get a key's value (first match) in a list of key-value pairs *)
let rec get_key_val ks l = match l with
  | [] -> failwith "not found"
  | (k, v)::xs -> if k = ks
  then v
  else get_key_val ks xs

let rec filter_by_keys kl l = match l with
  | [] -> []
  | (k, v)::xs -> if List.mem k kl
    then (k, v)::(filter_by_keys kl xs)
    else (filter_by_keys kl xs)
OCaml

Innovation. Community. Security.