[Haskell-beginners] Re: [Haskell-cafe] Defining a containing function on polymorphic list

Luke Palmer lrpalmer at gmail.com
Mon Dec 22 14:01:09 EST 2008

2008/12/22 Raeck Zhao <raeck at msn.com>

>  Thank you very much for your reply! It is really helpful!
> But I just found another 'problem', I just realize that the list does not
> support the user-defined data type?
> the list is also depending on the Eq function?
> For example,
> data Shape = Square | Triangle | Circle
> when I type either
> [Square, Triangle, Circle]

This is perfectly legal, but GHCi won't be able to print it, because there
is no Show instance for Shape.  You can declare one:

instance Show Shape where
    show Square = "Square"
    show Triagle = "Triangle"
    show Circle = "Circle"

This can be generated automatically when you declare the type, by using:

data Shape = Square | Triangle | Circle
    deriving (Show)

> or
> Square == Square

Similarly, to use (==), you need an Eq instance, which can be defined much
in the same way as the Show instance above  (deriving also works on Eq --
don't generalize too hastily; not all classes work with deriving).

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

More information about the Beginners mailing list