Consider the following method to create a start-at-1 enumeration in Haskell:
data Level = Lower | Middle | Upper
deriving (Show, Eq, Ord)
instance Enum Level where
toEnum 1 = Lower
toEnum 2 = Middle
toEnum 3 = Upper
fromEnum Lower = 1
fromEnum Middle = 2
fromEnum Upper = 3
instance Bounded Level where
minBound = Lower
maxBound = Upper
I’d rather not do the following:
data Level = DontUseThis | Lower | Middle | Upper
deriving (Show, Eq, Ord)
If not, is there a more straightforward way to do this?
First of all, you don’t need to define the
Boundedinstance yourself. If you addBoundedto the list of derived typeclasses you should get identical behavior.Secondly, the most straightforward way I can think of to accomplish this is to simply derive
Enumand then define your own translation functions. So something like this: