[Haskell-beginners] "Cyclic" instance of Enum
Patrick LeBoutillier
patrick.leboutillier at gmail.com
Tue Feb 1 20:14:05 CET 2011
Hi,
I've created an instance of Enum that repeats the same elements when
it gets to the end:
module Music.Note.Name where
data Name = C | D | E | F | G | A | B
deriving (Show, Eq)
instance Enum Name where
toEnum 0 = C
toEnum 1 = D
toEnum 2 = E
toEnum 3 = F
toEnum 4 = G
toEnum 5 = A
toEnum 6 = B
toEnum i = toEnum $ i `mod` 7
fromEnum C = 0
fromEnum D = 1
fromEnum E = 2
fromEnum F = 3
fromEnum G = 4
fromEnum A = 5
fromEnum B = 6
enumFromTo x y = map toEnum [a .. b']
where a = fromEnum x
b = fromEnum y
b' = if a <= b then b else b + 7
enumFromThen x1 x2 = error "enumFromThen not supported for Music.Note.Name"
enumFromThenTo x1 x2 y = error "enumFromThenTo not supported for
Music.Note.Name"
Does this violate any implicit rules that an Enum instance should
follow? Is there a better way to do this?
Thanks,
Patrick
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
More information about the Beginners
mailing list