English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
How to use Set Datatype
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-12-11 (21:10)
From: Brian Hurt <bhurt@s...>
Subject: Re: [Caml-list] How to use Set Datatype
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
		else if x > y then

module IntSet = Set.Make(Int);;

I hope this helps.