So I'm trying to build a function that takes a list of tuples and finds the tuple with the biggest second element. But I'm getting a pattern match error.
This is my code.
resultTuple :: [((Int,Int),Int)] -> (Int,Int)
resultTuple [] = error "something wrong"
resultTuple [x] = fst(x)
resultTuple (x:y:xs)
| snd(x) >= snd(y) = resultTuple(x:xs)
| snd(x) < snd(y) = resultTuple(y:xs)
This is my error.
Pattern match(es) are non-exhaustive
In an equation for ‘resultTuple’: Patterns not matched: (_:_:_)
All your cases for x:y:xs
have a condition and the compiler is warning you that you didn't cover the case where all the conditions are false. That is, the compiler is warning about the case where both snd x >= snd y
and snd x < snd y
are false.
Of course that can't actually happen, but the compiler does not realize that. To get rid of the warning, you can just replace the second condition with otherwise
.