[Haskell-beginners] A question about pattern matching
Brandon Allbery
allbery.b at gmail.com
Sat Feb 18 21:28:26 CET 2012
On Sat, Feb 18, 2012 at 15:16, bahadýr altan <doaltan at 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 at gmail.com
wandering unix systems administrator (available) (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120218/85975021/attachment.htm>
More information about the Beginners
mailing list