[Haskell-cafe] (newbie) instance Enum MyType where, smarter way?

Adrian Neumann aneumann at inf.fu-berlin.de
Tue Mar 27 11:15:58 EDT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

Hello,

I defined an enumeration datatype like this

data MyType = One | Two | Four | Eight

and want to make it an instance of the class Enum. deriving Enum won't
do what I want, as it labels the items 0,1,2,3. Is there a better way to
do this than

instance Enum MyType where
	fromEnum One = 1
	fromEnum Two = 2
	...
	toEnum 8 = Eight

Something like

instance Enum MyType where
	fromEnum One = 1
	fromEnum x = 2*pred x
	toEnum 1 = One
	toEnum x = succ (toEnum x `div` 2)

Which doesn't work because succ and pred are not (properly?) defined. Is
there a way to let deriving Enum do *some* of work (i.e. defining succ
and pred) while manually defining the other functions?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGCTRJ11V8mqIQMRsRA4GFAJwKaxQoKdEW91pHUskzJadDvh7lXgCeLg6P
dmQKcjRZEg4fqoZfQ4jOuhg=
=RWQ+
-----END PGP SIGNATURE-----


More information about the Haskell-Cafe mailing list