From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA18912; Wed, 23 Jul 2003 13:56:50 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id NAA27109 for ; Wed, 23 Jul 2003 13:56:48 +0200 (MET DST) Received: from pyramid.twistedmatrix.com (adsl-64-123-27-105.dsl.austtx.swbell.net [64.123.27.105]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h6NBulT27707 for ; Wed, 23 Jul 2003 13:56:47 +0200 (MET DST) Received: from chris by pyramid.twistedmatrix.com with local (Exim 3.35 #1 (Debian)) id 19fIEe-0007Uv-00 for ; Wed, 23 Jul 2003 06:56:44 -0500 Date: Wed, 23 Jul 2003 06:56:44 -0500 From: Chris Clearwater To: caml-list@inria.fr Subject: [Caml-list] Functor implementation Message-ID: <20030723115644.GA28291@pyramid.twistedmatrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.3.28i X-Loop: caml-list@inria.fr X-Spam: no; 0.00; functor:01 functors:01 implemented:01 ocaml's:01 chris:01 implements:01 bool:01 ocaml:01 rec:01 node:02 match:02 eof:02 tree:02 comparison:02 let:04 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hi all, I was curious as to how ocaml implements functors and finding no help with google, I thought I could ask on the list. After giving it a little thought I was thinking they might be implemented as follows: type 'a tree = Empty | Node of ('a tree) * 'a * ('a tree) type comparison = GT | EQ | LT let node l v r = Node(l, v, r) let single v = Node(Empty, v, Empty) let rec insert c t a = match t with Empty -> single a | Node(l, v, r) -> match (c a v) with LT -> node (insert c l a) v r | EQ -> t | GT -> node l v (insert c r a) let rec member c t a = match t with Empty -> false | Node(l, v, r) -> match (c a v) with LT -> member c l a | EQ -> true | GT -> member c r a (* Functor implementation follows *) type 'a setmodule = { empty: 'a tree; insert: 'a tree -> 'a -> 'a tree; member: 'a tree -> 'a -> bool; } let make c = { empty = Empty; insert = insert c; member = member c; } (* Use a functor *) let compare x y = let cmp = x - y in if cmp > 0 then GT else if cmp < 0 then LT else EQ let mymodule = make compare let s = mymodule.insert mymodule.empty 5 EOF How far off is Ocaml's way and is the performance comparable? And on a sidenote, wouldn't it be beneficial for there to be a built in comparison type and a function to convert from integers greater than less than or equal to, to the comparrison type? I imagine this would clarify code that uses comparisons greatly. Thanks in advance for your answers! ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners