From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr 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 17DDDBB81 for ; Sat, 19 Nov 2005 23:16:28 +0100 (CET) Received: from pih-relay04.plus.net (pih-relay04.plus.net [212.159.14.131]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id jAJMGRaf030132 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Sat, 19 Nov 2005 23:16:27 +0100 Received: from [80.229.56.224] (helo=chetara) by pih-relay04.plus.net with esmtp (Exim) id 1Edb0P-0008H1-6G for caml-list@yquem.inria.fr; Sat, 19 Nov 2005 22:16:21 +0000 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] how to use functors? Date: Sat, 19 Nov 2005 22:11:35 +0000 User-Agent: KMail/1.8.2 References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200511192211.35365.jon@ffconsultancy.com> X-Miltered: at nez-perce with ID 437FA43B.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 functors:01 beginners:01 functors:01 ocaml:01 ocaml:01 functor:01 sig:01 functor:01 sig:01 elt:01 val:01 val:01 bool:01 elt:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 On Saturday 19 November 2005 21:37, Jonathan Roewen wrote: > Hi, > > Prolly the wrong list, but I can't access caml.inria.fr's documentation ATM > =/ This is probably one for the beginners list. There is a brief explanation of functors on my "Benefits of OCaml" pages: http://www.ffconsultancy.com/free/ocaml/ under "Modules and Functors". Feed the functor a module with a sig that matches that required by the functor and it will spit out another module based upon the one you gave. For example, the built-in "String" module contains the "t" type and "compare" function definitions required by the "Set.Make" functor. So applying "String" to "Set.Make" gives you a module implementing sets of strings: # module SetOfStrings = Set.Make(String);; module SetOfStrings : sig type elt = String.t type t = Set.Make(String).t val empty : t val is_empty : t -> bool val mem : elt -> t -> bool val add : elt -> t -> t val singleton : elt -> t val remove : elt -> t -> t val union : t -> t -> t val inter : t -> t -> t val diff : t -> t -> t val compare : t -> t -> int val equal : t -> t -> bool val subset : t -> t -> bool val iter : (elt -> unit) -> t -> unit val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a val for_all : (elt -> bool) -> t -> bool val exists : (elt -> bool) -> t -> bool val filter : (elt -> bool) -> t -> t val partition : (elt -> bool) -> t -> t * t val cardinal : t -> int val elements : t -> elt list val min_elt : t -> elt val max_elt : t -> elt val choose : t -> elt val split : elt -> t -> t * bool * t end > How do I use a functor? For example: build a weak hashtable from Weak > module? Seems useful for building a cache... if only I knew how to use > it =P You may want something more controllable. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. Objective CAML for Scientists http://www.ffconsultancy.com/products/ocaml_for_scientists