On Sat, Feb 18, 2012 at 15:16, bahadýr altan <doaltan@yahoo.co.uk> wrote:
Hello. I'm wondering what " (Eq a) =>" part in the code below does.. I couldn't figure it myself.. Thanks in advance :)
isPalindrome :: (Eq a) => [a] -> Bool
isPalindrome xs = xs == (reverse xs)
It's a type constraint. In this case, it's saying that, rather than isPalindrome working only for a particular type, instead it works for any type "a" which has an implementation of the Eq class, where "a" is the type of the items in the passed list. Technically, it's telling the compiler to pass in the appropriate Eq implementation for whatever type isPalindrome is being applied to; this allows the correct implementation of (==) to be used. (And it really is passed in; it's not an accident that constraints look rather similar to parameters.)
--
brandon s allbery allbery.b@gmail.com
wandering unix systems administrator (available) (412) 475-9364 vm/sms