[Haskell-cafe] data Bin = Zero | One

Roel van Dijk vandijk.roel at gmail.com
Fri Oct 19 02:54:04 EDT 2007


To convert to and from integrals you could define two functions:

binToNum :: Num a => Bin -> a
binToNum Zero = 0
binToNum One = 1

numToBin :: Num a => a -> Bin
numToBin 0 = Zero
numToBin _ = One

Or you could derive Enum for your Bin type. This wil automatically
associate integers with the constructors of your type, in the order
given. (See section 10.2 of the haskell report:
http://www.haskell.org/onlinereport/derived.html)

data Bin = Zero | One deriving Enum

fromEnum Zero
> 0
fromEnum One
> 1

If this is not what you want, could you specify more precisely what
difficulty you ran into?


More information about the Haskell-Cafe mailing list