Haskell: No instance for (Eq a) arising from a use of `=='

前端 未结 2 949
春和景丽
春和景丽 2020-12-17 10:06
isPalindrome :: [a] -> Bool
isPalindrome xs = case xs of 
                        [] -> True 
                        [x] -> True
                        a          


        
2条回答
  •  温柔的废话
    2020-12-17 10:41

    hammar explanation is correct.

    Another simple example:

    nosPrimeiros :: a -> [(a,b)] -> Bool
    nosPrimeiros e [] = False
    nosPrimeiros e ((x,y):rl) = if (e==x)   then True
                                            else nosPrimeiros e rl
    

    The (e==x) will fail for this function signature. You need to replace:

    nosPrimeiros :: a -> [(a,b)] -> Bool
    

    adding an Eq instance for a

    nosPrimeiros :: Eq => a -> [(a,b)] -> Bool
    

    This instantiation is saying that now, a has a type that can be comparable with and (e==x) will not fail.

提交回复
热议问题