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

后端 未结 5 1040
感情败类
感情败类 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

    With Eq you can check if it's the last element.

    next :: (Eq a, Enum a, Bounded a) => a -> a
    next = bool minBound <$> succ <*> (/= maxBound)
    

提交回复
热议问题