Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to generate a list from a datatype's possible values in Haskell? [duplicate]

Can a list be generated from a type's possible values? E.g.

data Shape = Circle | Rectangle | Triangle | Pentagon

to

[Circle,Rectangle,Triangle,Pentagon]

like image 807
Christian Avatar asked Aug 14 '13 08:08

Christian


1 Answers

Yes it can. It's a duty of Enum and Bounded type classes e.g.

λ data Shape = Circle | Rectangle | Triangle | Pentagon deriving (Show, Enum, Bounded)
λ [minBound .. maxBound] :: [Shape]
[Circle,Rectangle,Triangle,Pentagon]
λ [minBound ..] :: [Shape]
[Circle,Rectangle,Triangle,Pentagon]
like image 126
Fedor Gogolev Avatar answered Oct 05 '22 22:10

Fedor Gogolev