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

Raeck Zhao raeck at msn.com
Mon Dec 22 09:35:01 EST 2008


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]

or 

Square == Square

there are errors!

So there is no way to construct a truly polymorphic List? any way to extend the list to support some user-defined data type?

Or...  I define the Shape in a wrong way actually?

Thanks

Raeck


Date: Mon, 22 Dec 2008 09:02:53 -0500
From: wagner.andrew at gmail.com
To: raeck at msn.com
Subject: Re: [Haskell-cafe] Defining a containing function on polymorphic list
CC: haskell-cafe at haskell.org; beginners at haskell.org

The problem here is even slightly deeper than you might realize. For example, what if you have a list of functions. How do you compare two functions to each other to see if they're equal? There is no good way really to do it! So, not only is == not completely polymorphic, but it CAN'T be. 


There is a nice solution for this, however, and it's very simple:

contain :: Eq a -> [a] -> Bool

contain x [] = False

contain x (y:ys) = if x == y then True else contain x ys

The "Eq a" in the type signature says that 'a' must be a member of the 'Eq' typeclass. That says, in turn, that 'a' must have == defined for it. Fortunately, most types have, or can easily derive that definition. Here is the definition of the typeclass:


class  Eq a  where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool

That is, for 'a' to be a member of 'Eq', it must have a == operator which can take 2 values of that type and return a Boolean, saying whether or not they're equal, and it must also have a definition for the /= operator, which is "not equal". These two are also defined in terms of each other, so if you define ==, you get /= for free, and vice versa. 


That's probably more information than you needed to know, but I hope it helps.

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









 
I am trying to define a containing function to see if a value is one
of the elements within a list which is polymorphic, but failed with the
following codes:

 > contain :: a -> [a] -> Bool

> contain x [] = False

> contain x (y:ys) = if x == y then True else contain x ys
it seems that the problem is the 'operator' == does not support a polymorphic check? 

Any way can solve the problem? or any alternative solution to achieve the purpose?

Thanks!

Raeck
 
It's the same Hotmail®. If by "same" you mean up to 70% faster. Get your account now.


_______________________________________________

Haskell-Cafe mailing list

Haskell-Cafe at haskell.org

http://www.haskell.org/mailman/listinfo/haskell-cafe




_________________________________________________________________
Life on your PC is safer, easier, and more enjoyable with Windows Vista®. 
http://clk.atdmt.com/MRT/go/127032870/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20081222/4762dfff/attachment.htm


More information about the Beginners mailing list