caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* generic data type -> int function
@ 2005-03-24 16:38 Hal Daume III
  2005-03-25 11:37 ` [Caml-list] " Jean-Christophe Filliatre
  2005-03-25 19:15 ` Kim Nguyen
  0 siblings, 2 replies; 11+ messages in thread
From: Hal Daume III @ 2005-03-24 16:38 UTC (permalink / raw)
  To: Caml Mailing List

Hi all --

Is there a straightforward way (or a built in function, or...) to 
automatically map an enumerated data type to integers (and back, if 
possible, but that's not strictly necessary).  In particular, I need 
something like:


type a = A | B | C | D
type b = E | F | G 
type c = H of a | I of b

and I would like to automatically have three functions:

let a_to_int = function
  A -> 0 | B -> 1 | C -> 2 | D -> 2

let b_to_int = function
  E -> 0 | F -> 1 | G -> 2

let c_to_int = function
   H a -> a_to_int a
 | I b -> 3 + b_to_int b

obviously in this case it's simple, but if now i add a "D2" to 'a', 
everything gets screwed up.

Is there a build in magic function that will do this, or some way to do 
this otherwis (like with camlp4 or something)?

Thanks in advance -- 

 - Hal


-- 
 Hal Daume III                                   | hdaume@isi.edu
 "Arrest this man, he talks in maths."           | www.isi.edu/~hdaume


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

end of thread, other threads:[~2005-03-31 18:05 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-24 16:38 generic data type -> int function Hal Daume III
2005-03-25 11:37 ` [Caml-list] " Jean-Christophe Filliatre
2005-03-30  3:26   ` Hal Daume III
2005-03-30 22:27     ` Oliver Bandel
2005-03-31 14:33       ` Hal Daume III
2005-03-31 17:01         ` Richard Jones
2005-03-31 18:04           ` Hal Daume III
2005-03-30 22:29     ` Oliver Bandel
2005-03-31 14:33       ` Hal Daume III
2005-03-25 19:15 ` Kim Nguyen
2005-03-29  7:29   ` Oliver Bandel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).