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"))
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)))
爲什麼發生這個錯誤?
你的函數假設'a's可以與'=='比較,你必須把這個信息放在類型簽名中。 – 2013-04-22 19:00:50
因爲'(==)'是'Eq'類的成員。所以你只能在'Eq'實例的類型中使用它。 – 2013-04-22 19:01:13
真實世界haskell chap 3?我也是! – Jason 2014-01-31 03:58:30