[Haskell-beginners] case statement and guarded equations

Rein Henrichs rein.henrichs at gmail.com
Fri Jul 7 22:42:10 UTC 2017


Sorry for the multiple responses. I just wanted to mention that the
equivalent guards do not "reduce pattern matching", they just move it
around a bit. Pattern matching is fundamental and pretty much everything
involved in evaluation desugars to pattern matching eventually.

On Fri, Jul 7, 2017 at 3:40 PM Rein Henrichs <rein.henrichs at gmail.com>
wrote:

> I'll also mention that GHC's exhaustiveness checker will mark the pattern
> match as exhaustive (since EvenOdd must be either Even or Odd and both
> cases are given) but warn about the two guards since it doesn't know that
> they form a dichotomy. You can use `otherwise`, which is just another name
> for True, to convince GHC that your guards are exhaustive.
>
> On Fri, Jul 7, 2017 at 3:38 PM Rein Henrichs <rein.henrichs at gmail.com>
> wrote:
>
>> Quoting the report[1], "A boolean guard, g, is semantically equivalent to
>> the pattern guard True <- g," which means the answer is "Yes". A boolean
>> guard is equivalent to a pattern match. A predicate involving ==, however,
>> introduces an Eq constraint that would not be required by pattern matching.
>> For a properly equivalent guard, you need to write your predicates using
>> pattern matching
>>
>> isEven Even = True
>> isEven _ = False
>>
>> to avoid the spurious Eq constraint.
>>
>> [1]
>> https://www.haskell.org/onlinereport/haskell2010/haskellch3.html#x8-460003.13
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170707/b4106325/attachment.html>


More information about the Beginners mailing list