Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: Micha <micha-1@fantasymail.de>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] How to use Set Datatype
Date: Sat, 11 Dec 2004 15:12:45 -0600 (CST)	[thread overview]
Message-ID: <Pine.LNX.4.44.0412111505110.28854-100000@localhost.localdomain> (raw)
In-Reply-To: <200412102044.39846.micha-1@fantasymail.de>

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



  parent reply	other threads:[~2004-12-11 21:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-10 18:37 Jan Stamer
2004-12-10 19:44 ` [Caml-list] " Micha
2004-12-11  1:33   ` Jon Harrop
2004-12-11 21:12   ` Brian Hurt [this message]
2004-12-12  0:05     ` skaller
2004-12-12  2:46       ` William Lovas
2004-12-12  3:09       ` Brian Hurt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.44.0412111505110.28854-100000@localhost.localdomain \
    --to=bhurt@spnz.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=micha-1@fantasymail.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox