[Haskell-cafe] a simple function

Henning Thielemann lemming at henning-thielemann.de
Tue Jul 12 10:52:37 EDT 2005

On Tue, 12 Jul 2005, wenduan wrote:

> Anyone please tell me what is wrong with the function:
> isEmpty ::[a]->Bool
> isEmpty xs | xs == []   = True
>                   |otherwise   =False
> When I tried to load it into the interpreter,it says the following:
>     Could not deduce (Eq a) from the context ()
>       arising from use of `==' at mylab2.hs:16

It means that the comparison with (==) only works for types of the Eq
class. This also applies to comparison with the empty list because there
are different types of empty lists. Thus you have to write

isEmpty :: Eq a => [a] -> Bool

But it is a bad solution. Better is

isEmpty [] = True
isEmpty _  = False

or even better: Use the standard function 'null'.

More information about the Haskell-Cafe mailing list