Is there some way to define an Enum in haskell that wraps around?

后端 未结 5 1024
感情败类
感情败类 2020-12-14 08:13

Consider I was designing a Monopoly game:

data Board = GO | A1 | CC1 | A2 | T1 | R1 | B1 | CH1 | B2 | B3 | 
  JAIL | C1 | U1 | C2 | C3 | R2 | D1 | CC2 | D2 |         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 08:59

    I know this is an old question but I just had this problem and I solved it this way.

    data SomeEnum = E0 | E1 | E2 | E3
                   deriving (Enum, Bounded, Eq)
    
    -- | a `succ` that wraps 
    succB :: (Bounded a, Enum a, Eq a) => a -> a 
    succB en | en == maxBound = minBound
             | otherwise = succ en
    
    -- | a `pred` that wraps
    predB :: (Bounded a, Enum a, Eq a) => a -> a
    predB en | en == minBound = maxBound
             | otherwise = pred en  
    

    The solution derives both Enum and Bounded but avoids abusing pred and succ as suggested.

    Incidently, I found that having

    allSomeEnum = [minBound..maxBound] :: [SomeEnum] 
    

    can be useful. That requires Bounded.

提交回复
热议问题