[Haskell-beginners] Meaning of (Eq a)
Johan Jeuring
johanj at cs.uu.nl
Sun Sep 5 05:18:42 EDT 2010
> RWH: chapter 3 - in question 5, you have to write a function which
> determines if a list is a palindrome. Here is my solution
>
> isPalindrome :: (Eq a) => [a] -> Bool
> isPalindrome [] = False
> isPalindrome x = compareLists x (reverse x)
> where compareLists [x] [y] = x == y
> compareLists (x:xs) (y:ys) = if x == y
> then compareLists xs ys
> else False
>
> Although it works, my question is why ghci refuses to run it without
> the "(Eq a) => " being added to the type signature of the function.
> Presumably, it is to let ghc know that you can perform equlity tests
> on a. If so, then why does the sumList function below work without any
> type signature of any kind? I haven't told ghc that the input list
> elements can be added together.
>
> sumList [] = 0
> sumList (x:xs) = x + sumList xs
Maybe GHC infers this for you?
Inspect the type of sumList by means of
>:t sumList
Cheers,
Johan
More information about the Beginners
mailing list