[Haskell-beginners] Pattern match(es) are overlapped ... but I do not see that they do

Michael Orlitzky michael at orlitzky.com
Sun Sep 1 05:39:22 CEST 2013


On 08/31/2013 11:07 PM, Edward Z. Yang wrote:
> Well, the trouble is at the source language level there
> is no way to tell if some_var is /actually/ a constant, or
> some complicated expression.  So you could do guards:
> 
>     case res of
>       _ | res == wxID_CANCEL -> True
>         | res == wxID_NO -> False
>         | ...
> 
> The suggestion to use an ADT is, you write a helper fucntion
> which does this case-split first, and then you do regular pattern
> matching on the result.  If you need to do this multiple times,
> it saves you a bunch of typing; it also gives you one place
> to write the error code when the integer is not one of these
> three values.
> 

In a perfect world, these constants would be defined as part of an
enumeration type, correct? For example,

  data WxId = WxIdCancel | WxIdNo | WxIdYes ... deriving (Enum)

in which case the original attempt would have succeeded since it would
be matching on a constructor.

However, unless there are constants defined for 0,1,... this approach
won't work automatically -- the derived Enum instance starts at zero and
increments by one. The library would have to define a custom Enum
instance and it would add a good bit of code.





More information about the Beginners mailing list