Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Design advice
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-09-30 (10:34)
From: Lauri Alanko <la@i...>
Subject: Re: [Caml-list] Design advice
This doesn't help you much, but you may be interested to know that in
Haskell this wouldn't be much of a problem. You would do simply:

data Suit = Spades | Hearts | Diamonds | Clubs
	deriving (Eq, Ord, Enum, Ix)

Here "Eq" and "Ord" mean that the compiler generates equality comparison
and ordering functions for the datatype. This is basically what ocaml's
(=) and compare -functions do.

"Enum" means that the compiler generates a bidirectional mapping between
your data type and a subset of natural numbers. This is useful if you
eg. need to communicate Suit values via an external interface (files,
sockets, whatnot).

Finally, "Ix" means that the compiler generates some functionality that
is required for a data type to be usable as an array index. And this is
probably the feature that you seem to need. In Haskell, you could have
an array whose _index_ type is Suit. With such an array, there's no fear
of overflowing, because there simply _aren't_ any index values of the
proper type that weren't included in the domain of the array.

Of course you can get the same safety in ocaml, too, but it just
requires some more work on your part.

This isn't an unconditional plug for Haskell, mind you. Both Haskell and
ocaml are fine languages, and each has great features that the other

Lauri Alanko
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: