2

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: (_:_:_)
melpomene
  • 84,125
  • 8
  • 85
  • 148
F.Lachlan
  • 37
  • 6
  • 1
    I suspect this is wrong, but maybe that guard needs a final case (maybe just `| _ = error "something wrong"`)? I think it looks like a complete set of patterns so I'm confused myself. – Andrea Oct 27 '18 at 12:03

1 Answers1

8

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.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 2
    It can actually happen if someone implements `Ord` improperly. For example, `Ord Double` is implemented in a way that makes `nan >= nan` and `nan < nan` both `False`. (Of course it can't happen in this specific case because `snd x :: Int`, but the compiler doesn't take that into account.) – melpomene Oct 27 '18 at 12:10