There are two ways to fix this. Let me see if I can get my syntax right this time :)<br><br>1.) Let GHC work out the Eq instance:<br> data Shape = Square | Triangle | Circle deriving Eq<br><br>2.) Tell GHC how to do it explicitly:<br>
data Shape = Square | Triangle | Circle<br> instance Eq Shape where<br> Square == Square = True<br> Triangle == Triangle = True<br> Circle == Circle = True<br> _ == _ = False<br><br>Note that the last line here means that any other comparisons are false.<br>
<br><div class="gmail_quote">On Mon, Dec 22, 2008 at 9:35 AM, Raeck Zhao <span dir="ltr"><<a href="mailto:raeck@msn.com">raeck@msn.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div>
Thank you very much for your reply! It is really helpful!<br><br>But I just found another 'problem', I just realize that the list does not support the user-defined data type?<br>the list is also depending on the Eq function?<br>
<br>For example,<br><br>data Shape = Square | Triangle | Circle<br><br>when I type either<br><br>[Square, Triangle, Circle]<br><br>or <br><br>Square == Square<br><br>there are errors!<br><br>So there is no way to construct a truly polymorphic List? any way to extend the list to support some user-defined data type?<br>
<br>Or... I define the Shape in a wrong way actually?<br><br>Thanks<br><br>Raeck<br><br><br><hr>Date: Mon, 22 Dec 2008 09:02:53 -0500<br>From: <a href="mailto:wagner.andrew@gmail.com" target="_blank">wagner.andrew@gmail.com</a><br>
To: <a href="mailto:raeck@msn.com" target="_blank">raeck@msn.com</a><br>Subject: Re: [Haskell-cafe] Defining a containing function on polymorphic list<br>CC: <a href="mailto:haskell-cafe@haskell.org" target="_blank">haskell-cafe@haskell.org</a>; <a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a><div class="Ih2E3d">
<br><br>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. <br>
<br>There is a nice solution for this, however, and it's very simple:<br><br>contain :: Eq a -> [a] -> Bool<br></div>
contain x [] = False<br>
contain x (y:ys) = if x == y then True else contain x ys<br><br>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:<br>
<br><span>class</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Eq.html#t:Eq" target="_blank">Eq</a> a <span>where</span><table cellpadding="0" cellspacing="0">
<tbody><tr><td><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Eq.html#v:%3D%3D" target="_blank">(==)</a> :: a -> a -> <a href="http://haskell.org/ghc/docs/latest/html/libraries/ghc-prim/GHC-Bool.html#t:Bool" target="_blank">Bool</a></td>
</tr><tr><td><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Eq.html#v:/%3D" target="_blank">(/=)</a> :: a -> a -> <a href="http://haskell.org/ghc/docs/latest/html/libraries/ghc-prim/GHC-Bool.html#t:Bool" target="_blank">Bool</a></td>
</tr></tbody></table><br>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. <br>
<br>That's probably more information than you needed to know, but I hope it helps.<br><br><div>2008/12/22 Raeck Zhao <span dir="ltr"><<a href="mailto:raeck@msn.com" target="_blank">raeck@msn.com</a>></span><br>
<blockquote style="padding-left: 1ex;">
<div>
<div>
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:<br>
> contain :: a -> [a] -> Bool<br>
> contain x [] = False<br>
> 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? <br>
Any way can solve the problem? or any alternative solution to achieve the purpose?<br>
Thanks!<br>
Raeck<br> </div><br><hr>It's the same Hotmail®. If by "same" you mean up to 70% faster. <a href="http://windowslive.com/online/hotmail?ocid=TXT_TAGLM_WL_hotmail_acq_broad1_122008" target="_blank">Get your account now.</a></div>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br><br><hr>Life on your PC is safer, easier, and more enjoyable with Windows Vista®. <a href="http://clk.atdmt.com/MRT/go/127032870/direct/01/" target="_blank">See how </a></div>
</blockquote></div><br>