Check whether it is palindrome (No instance for (Eq a) arising from a use of `==')

Asked

Viewed 152 times

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

1 answer

8


In your function you are comparing two instances of a type (generic) using ==. This imposes a restriction, implying that the type cannot be completely arbitrary. The type has to 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 function signature palin

palin :: Eq a => MList a -> Bool

Browser other questions tagged

You are not signed in. Login or sign up in order to post.