exhaustive pattern matches only because I left off `otherwise =`?

This question already has an answer here:

  • Why is GHC complaining about non-exhaustive patterns? 3 answers

  • GHC simply doesn't know that one of a > b , a < b , or a == b must evaluate to True . (Indeed, it is possible to write an Ord instance that violates this assumption -- though most programmers would not consider doing so, and certainly the one for Int will behave nicely in this regard.)

    You can make it patently obvious to GHC that you've covered all the cases by using a complete pattern match instead, eg

    respond correct guess = case compare guess correct of
        GT -> ...
        LT -> ...
        EQ -> ...
    

    GHC also has a special case in its exhaustiveness checker for the guards otherwise and True , so you may add (or replace) one of the guards with this as an alternative solution.

    respond correct guess
        | guess > correct = ...
        | guess < correct = ...
        | otherwise = ...
    

    The compiler does only a static semantic analysis. This is unable to see that the three cases cover all possible inputs because the meaning of the operators are determined at run-time.

    Your code is provably correct. If you want to avoid the warning from GHC, you can change the final condition to otherwise .

    链接地址: http://www.djcxy.com/p/43224.html

    上一篇: GHC的垃圾收集RTS选项

    下一篇: 穷尽模式匹配只是因为我遗漏了`否则=`?