File: prelude.ml

package info (click to toggle)
ocaml-doc 3.09-1
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 10,428 kB
  • ctags: 4,963
  • sloc: ml: 9,244; makefile: 2,413; ansic: 122; sh: 49; asm: 17
file content (32 lines) | stat: -rw-r--r-- 757 bytes parent folder | download | duplicates (3)
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
(**************** A few general-purpose functions *******************)

let fold f =
  let rec fold_rec a1 = function
  | [] -> (a1, [])
  | b1 :: bl ->
      let (a2, c2) = f a1 b1 in
      let (a, cl) = fold_rec a2 bl in
        (a, c2 :: cl) in
  fold_rec
;;

let try_find f =
  let rec try_find_rec = function
  | []  -> failwith "try_find"
  | a :: l -> try f a with Failure _ -> try_find_rec l in
  try_find_rec
;;

let partition p =
  let rec part_rec = function
  | []  -> ([], [])
  | a :: l -> let (pos, neg) = part_rec l in
              if p a then a :: pos, neg else pos, a :: neg in
  part_rec
;;

let union l1 l2 =
  List.fold_right (fun e u -> if List.mem e u then u else e :: u) l1 l2;;

let message s = print_string s; print_newline ()
;;