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

Gwern Branwen gwern0 at gmail.com
Tue Dec 23 23:13:38 EST 2008

Hash: SHA512

On Tue, Dec 23, 2008 at 10:48 PM, Henning Thielemann  wrote:
> Andrew Wagner schrieb:
>> 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
> Would HLint jump in here and suggest:
>   contain x (y:ys) = x == y || contain x ys
>  ? Or even "replace 'contain' by 'elem'" ?

I just tried it out. hlint makes no suggestions.

Incidentally, your syntax is wrong. Should be:

contain :: (Eq a) => a -> [a] -> Bool
contain _ [] = False
contain x (y:ys) = if x == y then True else contain x ys

- --
Version: GnuPG v1.4.9 (GNU/Linux)


More information about the Haskell-Cafe mailing list