File: sets.ml

package info (click to toggle)
jocaml 3.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 16,740 kB
  • sloc: ml: 107,815; ansic: 36,537; sh: 5,467; asm: 5,359; lisp: 4,041; makefile: 2,527; perl: 45; fortran: 21; sed: 19; cs: 9; tcl: 2
file content (39 lines) | stat: -rw-r--r-- 1,615 bytes parent folder | download
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
(***********************************************************************)
(*                                                                     *)
(*                           Objective Caml                            *)
(*                                                                     *)
(*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         *)
(*                                                                     *)
(*  Copyright 1996 Institut National de Recherche en Informatique et   *)
(*  en Automatique.  All rights reserved.  This file is distributed    *)
(*  under the terms of the Q Public License version 1.0.               *)
(*                                                                     *)
(***********************************************************************)

(* $Id: sets.ml 11113 2011-07-07 14:32:00Z maranget $ *)

module IntSet = Set.Make(struct type t = int let compare x y = x-y end)

let even = List.fold_right IntSet.add [0; -2; 2; 4; 6; -10] IntSet.empty

let odd = List.fold_right IntSet.add [9; -7; 5; 1; -3] IntSet.empty

let _ =
  for i = -10 to 10 do
    Printf.printf "%d  %B  %B\n" i (IntSet.mem i even) (IntSet.mem i odd)
  done

module PowerSet(BaseSet: Set.S)
               (SetOrd: functor(S: Set.S) -> Set.OrderedType) =
  Set.Make(SetOrd(BaseSet))

module IntSetSet = PowerSet(IntSet)(functor (S: Set.S) -> S)

let setofset = List.fold_right IntSetSet.add [even; odd] IntSetSet.empty

let _ =
  List.iter
    (fun s -> Printf.printf "%B\n" (IntSetSet.mem s setofset))
    [IntSet.empty; even; odd; IntSet.union even odd]

let _ = exit 0