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

Nathan Hüsken nathan.huesken at posteo.de
Sun Sep 1 09:36:36 CEST 2013


Hey,

I ended up using guards. If I end up needing this at multiple locations, 
I will introduce an ADT.
This was very helpful!

Regards,
Nathan

On 09/01/2013 05:39 AM, Michael Orlitzky wrote:
> 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.
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners





More information about the Beginners mailing list