7

可以从类型的可能值生成列表吗?例如

data Shape = Circle | Rectangle | Triangle | Pentagon

[Circle,Rectangle,Triangle,Pentagon]

4

1 回答 1

20

是的,它可以。这是枚举有界类型类的职责,例如

λ data Shape = Circle | Rectangle | Triangle | Pentagon deriving (Show, Enum, Bounded)
λ [minBound .. maxBound] :: [Shape]
[Circle,Rectangle,Triangle,Pentagon]
λ [minBound ..] :: [Shape]
[Circle,Rectangle,Triangle,Pentagon]
于 2013-08-14T08:32:30.063 回答