From 7039df9ad24be112cf85294abe4886f02148c6df Mon Sep 17 00:00:00 2001 From: Sebastien Ferre Date: Wed, 23 Jul 2003 18:11:12 +0000 Subject: [PATCH] Add of 'comp' and 'of_list'. --- lSet.ml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lSet.ml b/lSet.ml index f3773a5..b01b6a6 100644 --- a/lSet.ml +++ b/lSet.ml @@ -15,6 +15,31 @@ let is_empty : 'a list -> bool = function [] -> true | _ -> false (* return the cardinal of a set *) let cardinal : 'a list -> int = List.length +(* comparison of 2 sets for any relation among: Contains, Contained, Equals, Other *) + +type comp = Contains | Contained | Equals | Other + +let rec comp : 'a list -> 'a list -> comp = + fun l1 l2 -> match l1, l2 with + | [], [] -> Equals + | _, [] -> Contains + | [], _ -> Contained + | x1::l1, x2::l2 -> + let c = compare x1 x2 in + if c < 0 then + match comp l1 (x2::l2) with + | Equals + | Contains -> Contains + | Contained + | Other -> Other + else if c > 0 then + match comp (x1::l1) l2 with + | Equals + | Contained -> Contained + | Contains + | Other -> Other + else comp l1 l2 + (* return true if the first set contains the second *) let rec contains : 'a list -> 'a list -> bool = fun l1 l2 -> match l1, l2 with @@ -49,6 +74,9 @@ let add : 'a -> 'a list -> 'a list = let singleton : 'a -> 'a list = fun x -> [x] +let of_list : 'a list -> 'a t = + fun l -> List.fold_left (fun res x -> add x res) [] l + (* return the intersection of 2 sets *) let rec inter : 'a list -> 'a list -> 'a list = fun l1 l2 -> match l1, l2 with -- 2.1.1