Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Pattern match(es) are overlapped ... but I do not see
      that they do (Michael Orlitzky)
   2. Re:  Pattern match(es) are overlapped ... but I do not see
      that they do (Nathan H?sken)


----------------------------------------------------------------------

Message: 1
Date: Sat, 31 Aug 2013 23:39:22 -0400
From: Michael Orlitzky <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Pattern match(es) are overlapped ...
        but I do not see that they do
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

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.




------------------------------

Message: 2
Date: Sun, 01 Sep 2013 09:36:36 +0200
From: Nathan H?sken <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Pattern match(es) are overlapped ...
        but I do not see that they do
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners




------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 63, Issue 2
****************************************

Reply via email to