(* * Sets. * * ---------------------------------------------------------------- * * @begin[license] * Copyright (C) 2002 Jason Hickey, Caltech * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * Author: Jason Hickey * @email{jyh@cs.caltech.edu} * @end[license] *) module type EltSig = sig type elt val compare : elt -> elt -> int end module type SetSig = sig type elt type set val empty : set val mem : elt -> set -> bool val add : elt -> set -> set end module MakeSet (Elt : EltSig) : SetSig with type elt = Elt.elt = struct type elt = Elt.elt type set = Leaf | Node of elt * set * set let empty = Leaf let rec mem x s = match s with Leaf -> false | Node (x', left, right) -> let cmp = Elt.compare x x' in if cmp < 0 then mem x left else if cmp > 0 then mem x right else true let rec add x s = match s with Leaf -> Node (x, Leaf, Leaf) | Node (x', left, right) -> let cmp = Elt.compare x x' in if cmp < 0 then Node (x', add x left, right) else if cmp > 0 then Node (x', left, add x right) else s end (*! * @docoff * * -*- * Local Variables: * Caml-master: "compile" * End: * -*- *)