two easy questions

Dean Herington heringto@cs.unc.edu
Thu, 20 Feb 2003 12:40:14 -0500


Oops!  Small bug.  See below.

Dean Herington wrote:

> You can't derive Enum Player automatically, but you can program it.  Here's one
> way how, using shorter example names.
>
> -- Dean
>
> data E1 = E1a | E1b | E1c deriving (Enum, Bounded, Show, Read)
> data E2 = E2a | E2b | E2c deriving (Enum, Bounded, Show, Read)
>
> data E = E1 E1 | E2 E2    deriving (Show, Read)
>
> instance Enum E where
>   fromEnum (E1 e1) =           fromEnum e1
>   fromEnum (E2 e2) = size_E1 + fromEnum e2
>   toEnum i | i < size_E1 = E1 (toEnum i)
>            | otherwise   = E2 (toEnum (i - size_E1))
>
> instance Bounded E where
>   minBound = toEnum 0
>   maxBound = toEnum (size_E1 + size_E2)

Should be:
  maxBound = toEnum (size_E1 + size_E2 - 1)

> size_E1 = fromEnum (maxBound :: E1) + 1
> size_E2 = fromEnum (maxBound :: E2) + 1