Verify if it is palindrome (No instance for (Eq a) arising from a use of '==')

5

I got the second error in the code:

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

Code:

--Verifica se a lista é um palíndromo
palin :: MList a -> Bool
palin(x)
    |x==reverter(x) = True

--Reversão de lista na cauda
reverter:: (MList a) -> (MList a)
reverter(x) = reverteraux(x, Nil)
    where
         reverteraux:: (MList a, MList a) -> MList a
         reverteraux((Cons h t), a) = reverteraux( t, (Cons h a))
         reverteraux(Nil, a) = a
    
asked by anonymous 25.05.2015 / 00:49

1 answer

8

In your role you are comparing two instances of a (generic) type using ==. This imposes a constraint, implies that the type can not be completely arbitrary. The type must be an instance of Eq , since the signature of == is (==) :: Eq a => a -> a -> Bool .

You can fix the problem by adding a constraint on your signature of the palin function

palin :: Eq a => MList a -> Bool
    
26.05.2015 / 14:06