[Haskell-beginners] Re: [Haskell-cafe] 'Iterating a data type'

Luke Palmer lrpalmer at gmail.com
Mon Dec 29 20:28:46 EST 2008


On Mon, Dec 29, 2008 at 6:24 PM, <raeck at msn.com> wrote:

> Are there anyway to express the "iterating" of a user-defined data type in
> Haskell?
>
> For example, in
>
>  data Shape = Square | Circle | Triangle
>>
>
> how can I 'iterate' them and apply them all to the same function without
> indicating them explicitly?
> such as [func Square, func Circle, func Triangle].
> Can I use a form similar to the following case in a list instead:
>
>  Numbers = [1,2,3,4,5]
>>
>
>  [func x | x <- Numbers ]
>>
>
> Actually what I want is to obtain all the possible values of a data type
> (Shape).


For "enum" style data, where all the constructors have no arguments, you can
do deriving (Bounded, Enum), so:

data Shape = Square | Circle | Triangle
  deriving (Bounded,Enum)

Then:

numbers = [minBound..maxBound] :: [Shape]

Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20081229/a5358a1f/attachment.htm


More information about the Beginners mailing list