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

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

    A simpler solution than nanothief:

    nextBoard :: Board -> Board
    nextBoard H2 = GO
    nextBoard t = succ t
    

    I don't think you'll be able to use Enum directly for what you want, but this solution quickly wraps it to form the behaviour you want.

提交回复
热议问题