From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 5F197BB91 for ; Wed, 26 Jan 2005 15:30:19 +0100 (CET) Received: from ptb-relay03.plus.net (ptb-relay03.plus.net [212.159.14.214]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j0QEUI4I012555 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 26 Jan 2005 15:30:19 +0100 Received: from [80.229.56.224] (helo=chetara) by ptb-relay03.plus.net with esmtp (Exim) id 1CtoTN-000IXk-7y for caml-list@yquem.inria.fr; Wed, 26 Jan 2005 14:48:45 +0000 From: Jon Harrop Organization: University of Cambridge To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] 'a Set? Date: Wed, 26 Jan 2005 09:13:08 +0000 User-Agent: KMail/1.7.1 References: <727068A7-6F2C-11D9-8411-0003939A19AA@fas.harvard.edu> In-Reply-To: <727068A7-6F2C-11D9-8411-0003939A19AA@fas.harvard.edu> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200501260913.09018.jon@jdh30.plus.com> X-Miltered: at nez-perce with ID 41F7A97A.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 wrote:01 functor:01 elt:01 higher-order:01 bool:01 bool:01 rec:01 rec:01 closures:01 cheers:01 polymorphic:01 functions:01 functions:01 argument:01 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.0 X-Spam-Level: On Tuesday 25 January 2005 23:54, Mike Hamburg wrote: > Is there a clean way to do this without removing the code from set.ml > and modifying it? I do not believe so. I have also had to do this. Compared to a flat set of functions, the functor approach has the advantage of enforcing a consistently used compare function. The same effect can be achieved with "elt = 'a" by writing a higher-order function which returns a record containing the Set.* functions using the given function argument as the compare function. Something equivalent to this: type 'a t = 'a list type 'a set = { empty : 'a t; is_empty : 'a t -> bool; add : 'a -> 'a t -> 'a t; mem : 'a -> 'a t -> bool } let rec add compare e = function [] -> [e] | h :: t -> match compare h e with -1 -> e :: h :: t | 0 -> e :: t | _ -> h :: add compare e t let rec mem compare e = function [] -> false | h :: t -> match compare h e with -1 -> false | 0 -> true | _ -> mem compare e t let make ?(compare=compare) () = { empty = []; is_empty = (fun s -> s=[]); add = add compare; mem = mem compare } Possible issues with this are that building closures (i.e. in "make") is expensive and that the resulting type is monomorphic ('_a). You can probably get a polymorphic type most easily by putting the definitions of "add" etc. in the record definition, rather than partially applying their arguments. Cheers, Jon.