isPalindrome :: [a] -> Bool
isPalindrome xs = case xs of
[] -> True
[x] -> True
a -> (last a) == (head a) && (isPalindrome (drop 1 (take (length a - 1) a)))
main = do
print (show (isPalindrome "blaho"))
results in
No instance for (Eq a)
arising from a use of `=='
In the first argument of `(&&)', namely `(last a) == (head a)'
In the expression:
(last a) == (head a)
&& (isPalindrome (drop 1 (take (length a - 1) a)))
In a case alternative:
a -> (last a) == (head a)
&& (isPalindrome (drop 1 (take (length a - 1) a)))
Why is this error occurring?
a
s can be compared with==
, you have to put this info in the type signature. – Karoly Horvath(==)
is a member of theEq
class. So you can use it only on types that are instances ofEq
. – Daniel Fischer