Elements of a List are all the same

2

I made this function definition equal, which checks to see if all the elements of a list are equal.

iguais :: [Float] -> Bool

iguais [x,y] = x == y  

iguais (h:t) = h == (head t) 

However, the result is not what you want. Can you tell what the error is?

    
asked by anonymous 18.12.2017 / 17:30

1 answer

1

Your code does not work because you are not recursively comparing all the elements.

Try this version here:

iguais :: [Float] -> Bool
iguais [] = True
iguais [_] = True
iguais (x:xs) = x == (head xs) && iguais xs -- chamada recursiva nesta linha
    
18.12.2017 / 19:32