Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* How to use Set Datatype
@ 2004-12-10 18:37 Jan Stamer
  2004-12-10 19:44 ` [Caml-list] " Micha
  0 siblings, 1 reply; 7+ messages in thread
From: Jan Stamer @ 2004-12-10 18:37 UTC (permalink / raw)
  To: caml-list

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.

I would like to use a set of Strings. Can anybody help me and give me a 
few lines of sample code?

Cheers,
Jan

-- 
************************************
    Jan Stamer
    Katharinenstr. 15
    79104 Freiburg

email:  Jan.Stamer@neptun.uni-freiburg.de
phone:  +49-(0)-761-4808167
mobile: +49-(0)-178-4155553


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] How to use Set Datatype
  2004-12-10 18:37 How to use Set Datatype Jan Stamer
@ 2004-12-10 19:44 ` Micha
  2004-12-11  1:33   ` Jon Harrop
  2004-12-11 21:12   ` Brian Hurt
  0 siblings, 2 replies; 7+ messages in thread
From: Micha @ 2004-12-10 19:44 UTC (permalink / raw)
  To: caml-list

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);;

now you can use it (silly example :-)

let set_example str = 
 let set = StringSet.empty in    (* make new set *) 
 let set = StringSet.add set str in
StringSet.mem set "xxx";;    


cheers
 Michael


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] How to use Set Datatype
  2004-12-10 19:44 ` [Caml-list] " Micha
@ 2004-12-11  1:33   ` Jon Harrop
  2004-12-11 21:12   ` Brian Hurt
  1 sibling, 0 replies; 7+ messages in thread
From: Jon Harrop @ 2004-12-11  1:33 UTC (permalink / raw)
  To: caml-list

On Friday 10 December 2004 19:44, Micha wrote:
> module StringSet = Set.Make (struct type t = string   let compare = compare
>  end);;

As the "String" module implements both a type "t" and a "compare" function, 
you can just write:

# module StringSet = Set.Make(String);;
...

Cheers,
Jon.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] How to use Set Datatype
  2004-12-10 19:44 ` [Caml-list] " Micha
  2004-12-11  1:33   ` Jon Harrop
@ 2004-12-11 21:12   ` Brian Hurt
  2004-12-12  0:05     ` skaller
  1 sibling, 1 reply; 7+ messages in thread
From: Brian Hurt @ 2004-12-11 21:12 UTC (permalink / raw)
  To: Micha; +Cc: caml-list

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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] How to use Set Datatype
  2004-12-11 21:12   ` Brian Hurt
@ 2004-12-12  0:05     ` skaller
  2004-12-12  2:46       ` William Lovas
  2004-12-12  3:09       ` Brian Hurt
  0 siblings, 2 replies; 7+ messages in thread
From: skaller @ 2004-12-12  0:05 UTC (permalink / raw)
  To: Brian Hurt; +Cc: Micha, caml-list

On Sun, 2004-12-12 at 08:12, Brian Hurt wrote:
> On Fri, 10 Dec 2004, Micha wrote:

> Another thing- longtime Ocaml programmers tend to inline their structure 
> definitions.  

Why? Because binding implicitly by name is basically bogus?
So you write

	Set.Make(struct let compare=String.compare ...

because the name in the functor 'compare' only agrees
accidentally. The anonymous struct there seems general,
and at least exposes explicitly the bindings.

Of course the *implicit* binding in C++ templates (dependent
name lookup) is something some of us have learned to hate
intensely.. especially when the template calls T().compare ..
and our input class spelled it 'Compare' ..


-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] How to use Set Datatype
  2004-12-12  0:05     ` skaller
@ 2004-12-12  2:46       ` William Lovas
  2004-12-12  3:09       ` Brian Hurt
  1 sibling, 0 replies; 7+ messages in thread
From: William Lovas @ 2004-12-12  2:46 UTC (permalink / raw)
  To: caml-list

On Sun, Dec 12, 2004 at 11:05:42AM +1100, skaller wrote:
> On Sun, 2004-12-12 at 08:12, Brian Hurt wrote:
> > On Fri, 10 Dec 2004, Micha wrote:
> 
> > Another thing- longtime Ocaml programmers tend to inline their structure 
> > definitions.  
> 
> Why? Because binding implicitly by name is basically bogus?

What do you mean by bogus?  And what do you mean by "binding implicitly by
name"?

I think experienced O'Caml programmers simply prefer to save the step of
binding, especially if module you're passing as the argument to a functor
is never used elsewhere.

> So you write
> 
> 	Set.Make(struct let compare=String.compare ...
> 
> because the name in the functor 'compare' only agrees
> accidentally. The anonymous struct there seems general,
> and at least exposes explicitly the bindings.

There's nothing "accidental" about it -- there is a very precise sense in
which some modules are appropriate as input to the Set.Make functor, namely
that they have the signature Set.OrderedType.  This is just as precise as
saying that 3 is appropriate as an argument to (+) while "hello" is not.

O'Caml's module system is not some arbitrary hack -- it has a very rich and
elegant type theoretic foundation.

William


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] How to use Set Datatype
  2004-12-12  0:05     ` skaller
  2004-12-12  2:46       ` William Lovas
@ 2004-12-12  3:09       ` Brian Hurt
  1 sibling, 0 replies; 7+ messages in thread
From: Brian Hurt @ 2004-12-12  3:09 UTC (permalink / raw)
  To: skaller; +Cc: Micha, caml-list

On 12 Dec 2004, skaller wrote:

> On Sun, 2004-12-12 at 08:12, Brian Hurt wrote:
> > On Fri, 10 Dec 2004, Micha wrote:
> 
> > Another thing- longtime Ocaml programmers tend to inline their structure 
> > definitions.  
> 
> Why? Because binding implicitly by name is basically bogus?

Yes.  But it makes the functor application look complicated and clunky to 
a newbie, when it really isn't.  Once you're familiar with the syntax, 
inlining becomes a better option.

Brian



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-12-12  3:07 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-10 18:37 How to use Set Datatype Jan Stamer
2004-12-10 19:44 ` [Caml-list] " Micha
2004-12-11  1:33   ` Jon Harrop
2004-12-11 21:12   ` Brian Hurt
2004-12-12  0:05     ` skaller
2004-12-12  2:46       ` William Lovas
2004-12-12  3:09       ` Brian Hurt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox