From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id B9E21BB81 for ; Sat, 11 Dec 2004 22:10:29 +0100 (CET) Received: from herd.plethora.net (herd.plethora.net [205.166.146.1]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id iBBLARNN003380 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Sat, 11 Dec 2004 22:10:29 +0100 Received: from bhurt.plethora.net (bhurt.plethora.net [205.166.146.49]) by herd.plethora.net (8.13.1/8.12.11) with ESMTP id iBBLA9se000046 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sat, 11 Dec 2004 15:10:12 -0600 (CST) Date: Sat, 11 Dec 2004 15:12:45 -0600 (CST) From: Brian Hurt X-X-Sender: bhurt@localhost.localdomain To: Micha Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] How to use Set Datatype In-Reply-To: <200412102044.39846.micha-1@fantasymail.de> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Miltered: at concorde with ID 41BB6243.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 datatype:01 wrote:01 ocaml:01 datatype:01 stringset:01 struct:01 functor:01 ocaml:01 stringset:01 integers:01 struct:01 10.:98 ...:98 strings: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 Fri, 10 Dec 2004, Micha wrote: > Am Freitag, 10. Dezember 2004 19:37 schrieb Jan Stamer: > > Hi all, > > > > I am new to Ocaml and for the past hour I tried to figure out how to use > > the built-in Set Datatype. > > :-) easy to spend time with this ... > > > I would like to use a set of Strings. Can anybody help me and give me a > > few lines of sample code? > > you have to make a module first, which holds the type and the order function > for your set: > > module StringSet = Set.Make (struct type t = string let compare = compare > end);; An interesting point here. When being passed to a Functor (which is what Set.Make is), Ocaml does structural comparison of types. This means that any module that has a type t and a compare function works. For example, the String module works just fine. Try: module StringSet = Set.Make(String);; Another thing- longtime Ocaml programmers tend to inline their structure definitions. If we wanted to make set of integers, a cleaner (more understandable to the newbie) way to code this might be: module Int = struct type t = int let compare (x: t) y = if x < y then -1 else if x > y then 1 else 0 end;; module IntSet = Set.Make(Int);; I hope this helps. Brian