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

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


        
2条回答
  •  独厮守ぢ
    2020-12-17 10:40

    You're comparing two items of type a using ==. That means a can't just be any type - it has to be an instance of Eq, as the type of == is (==) :: Eq a => a -> a -> Bool.

    You can fix this by adding an Eq constraint on a to the type signature of your function:

    isPalindrome :: Eq a => [a] -> Bool
    

    By the way, there is a much simpler way to implement this function using reverse.

提交回复
热议问题