Better Way to Define an Enum in Haskell

前端 未结 5 628
囚心锁ツ
囚心锁ツ 2021-02-01 01:48

I want a datatype to represent a finite set of integers that can be addressed by specific names. I figure the best way to do that is to use an Enum.

However, there is o

5条回答
  •  面向向阳花
    2021-02-01 02:01

    instance Enum MyDataType where
        fromEnum = fromJust . flip lookup table
        toEnum = fromJust . flip lookup (map swap table)
    table = [(Foo, 0), (Bar, 1), (Baz, 2)]
    

提交回复
热议问题