data Color = Red | Orange | Yellow | Green | Blue | Indigo | Violet
deriving Show
-- deriving (Eq, Enum, Ord, Show)
-- Consult Prelude.hs to know about the Minimal complete definition for each class.
instance Eq Color where -- Minimal complete definition: (==) or (/=)
Red == Red = True
Orange == Orange = True
Yellow == Yellow = True
Green == Green = True
Blue == Blue = True
Indigo == Indigo = True
Violet == Violet = True
_ == _ = False
instance Ord Color where -- Minimal complete definition: (<=) or compare
Red < Orange = True
Red < Yellow = True
Red < Green = True
Red < Blue = True
Red < Indigo = True
Red < Violet = True
Orange < Yellow = True
Orange < Green = True
Orange < Blue = True
Orange < Indigo = True
Orange < Violet = True
Yellow < Green = True
Yellow < Blue = True
Yellow < Indigo = True
Yellow < Violet = True
Green < Blue = True
Green < Indigo = True
Green < Violet = True
Blue < Indigo = True
Blue < Violet = True
Indigo < Violet = True
_ < _ = False
a <= b = (a < b) || (a == b)
instance Enum Color where -- Minimal complete definition: toEnum, fromEnum
fromEnum Red = 0
fromEnum Orange = 1
fromEnum Yellow = 2
fromEnum Green = 3
fromEnum Blue = 4
fromEnum Indigo = 5
fromEnum Violet = 6
toEnum 0 = Red
toEnum 1 = Orange
toEnum 2 = Yellow
toEnum 3 = Green
toEnum 4 = Blue
toEnum 5 = Indigo
toEnum 6 = Violet
{-
Redefine the two methods below to make sure
something like [Red ..], [Indigo, Green ..] just works
as we deriving (Enum).
-}
enumFrom x = map toEnum [ fromEnum x .. 6]
enumFromThen x y =
let x' = fromEnum x
y' = fromEnum y
z' = if (x' > y')
then 0
else if (x' < y')
then 6
else x'
in map toEnum [x', y' .. z']
This entry was posted
on Saturday, June 30th, 2007 at 2:58 pm and is filed under Haskell - SOE.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.