Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1.  Early return in IO monad (Nathan H?sken)
   2. Re:  Early return in IO monad (Brent Yorgey)
   3. Re:  Early return in IO monad (Tidus Zero)
   4.  Pattern match(es) are overlapped ... but I do    not see that
      they do (Nathan H?sken)
   5. Re:  Pattern match(es) are overlapped ... but I   do not see
      that they do (Edward Z. Yang)
   6. Re:  Pattern match(es) are overlapped ... but I do not see
      that they do (Nathan H?sken)
   7. Re:  Early return in IO monad (Tony Morris)
   8. Re:  Pattern match(es) are overlapped ... but I   do not see
      that they do (Edward Z. Yang)


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

Message: 1
Date: Sat, 31 Aug 2013 20:05:26 +0200
From: Nathan H?sken <nathan.hues...@posteo.de>
To: Haskell Beginners Mailinglist <beginners@haskell.org>
Subject: [Haskell-beginners] Early return in IO monad
Message-ID: <52223066.7010...@posteo.de>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hey,

Is it somehow possible to return "early" in a do block of the IO monad?
The eqivalent do in C:

void doBlock() {
     if (some preCondition) {
       return;
     }
     ...
}

???

Thanks!
Nathan



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

Message: 2
Date: Sat, 31 Aug 2013 14:23:16 -0400
From: Brent Yorgey <byor...@seas.upenn.edu>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Early return in IO monad
Message-ID: <20130831182316.ga23...@seas.upenn.edu>
Content-Type: text/plain; charset=iso-8859-1

On Sat, Aug 31, 2013 at 08:05:26PM +0200, Nathan H?sken wrote:
> Hey,
> 
> Is it somehow possible to return "early" in a do block of the IO monad?
> The eqivalent do in C:
> 
> void doBlock() {
>     if (some preCondition) {
>       return;
>     }
>     ...
> }
> 
> ???

Nope.  But you can say

  do ...
     when (not (some condition)) $ do
       the
       rest
       of
       the
       stuff

You can even indent "the rest of the stuff" directly under the 'when'
if you like, though IMO that may be a bit confusing.

-Brent



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

Message: 3
Date: Sat, 31 Aug 2013 22:03:35 +0200
From: Tidus Zero <gnuso...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Cc: Haskell Beginners Mailinglist <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Early return in IO monad
Message-ID: <262eaf29-f84b-40be-9664-50aa29172...@gmail.com>
Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes

You can use the EitherT monad with IO and use the 'left' function  
which is just 'fail' to return early if you aren't restricted to only  
use the IO monad.

Sent from my iGNU

On 31. aug. 2013, at 20.05, Nathan H?sken <nathan.hues...@posteo.de>  
wrote:

> Hey,
>
> Is it somehow possible to return "early" in a do block of the IO  
> monad?
> The eqivalent do in C:
>
> void doBlock() {
>    if (some preCondition) {
>      return;
>    }
>    ...
> }
>
> ???
>
> Thanks!
> Nathan
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



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

Message: 4
Date: Sun, 01 Sep 2013 00:46:01 +0200
From: Nathan H?sken <nathan.hues...@posteo.de>
To: Haskell Beginners Mailinglist <beginners@haskell.org>
Subject: [Haskell-beginners] Pattern match(es) are overlapped ... but
        I do    not see that they do
Message-ID: <52227229.1000...@posteo.de>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hey,

I have the following code (which uses wxHaskell):

import Graphics.UI.WX
import Graphics.UI.WX.Controls
import Graphics.UI.WXCore
import Graphics.UI.WXCore.Events

(...)

dealWithUnsavedChanges :: Var ProgramState -> TextCtrl a -> IO Bool
dealWithUnsavedChanges state tc = do
   ProgramState unsavedChanges _ <- varGet state
   if not unsavedChanges then return True else do
     res <- messageDialog tc "Unsaved changes ..." "You have unsaved 
changes, do you want to save them?" (wxYES_NO .+. wxCANCEL .+. 
wxICON_EXCLAMATION)
     case res of
       wxID_CANCEL -> return False
       wxID_NO     -> return True
       wxID_YES    -> do
         onSave state tc
         -- check if saving worked
         ProgramState newUnsavedChanges _ <- varGet state
         return (not newUnsavedChanges)

When I compile it, I get:

Main.hs:130:5: Warning:
     Pattern match(es) are overlapped
     In a case alternative:
         wxID_NO -> ...
         wxID_YES -> ...

which is strange. I checked, wxID_NO is defined as 5104 and wxID_YES as 
5103. So they are not overlapping. On the other hand, when I replace the 
wxID_NO/YES/CANCEL with the actual values, the warning vanishes.

Any Idea what this could be?

Thanks!
Nathan



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

Message: 5
Date: Sat, 31 Aug 2013 16:12:16 -0700
From: "Edward Z. Yang" <ezy...@mit.edu>
To: Nathan H?sken <nathan.hues...@posteo.de>
Cc: Haskell Beginners Mailinglist <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Pattern match(es) are overlapped ...
        but I   do not see that they do
Message-ID: <1377990618-sup-4807@javelin>
Content-Type: text/plain; charset=UTF-8

Hello Nathan,

The problem is that because these "numbers" are actually
variable names, Haskell is not pattern matching against the
number; it is actually binding the result type to the variable.
You might as well have written:

>      case res of
>        x -> return False
>        x -> return True
>        x -> do

which of course is overlapping.

There are a few ways to rewrite your program, but my recommendation
would be to define an abstract data type which encodes the three
choices, and pattern match against that.

Edward

Excerpts from Nathan H?sken's message of Sat Aug 31 15:46:01 -0700 2013:
> Hey,
> 
> I have the following code (which uses wxHaskell):
> 
> import Graphics.UI.WX
> import Graphics.UI.WX.Controls
> import Graphics.UI.WXCore
> import Graphics.UI.WXCore.Events
> 
> (...)
> 
> dealWithUnsavedChanges :: Var ProgramState -> TextCtrl a -> IO Bool
> dealWithUnsavedChanges state tc = do
>    ProgramState unsavedChanges _ <- varGet state
>    if not unsavedChanges then return True else do
>      res <- messageDialog tc "Unsaved changes ..." "You have unsaved 
> changes, do you want to save them?" (wxYES_NO .+. wxCANCEL .+. 
> wxICON_EXCLAMATION)
>      case res of
>        wxID_CANCEL -> return False
>        wxID_NO     -> return True
>        wxID_YES    -> do
>          onSave state tc
>          -- check if saving worked
>          ProgramState newUnsavedChanges _ <- varGet state
>          return (not newUnsavedChanges)
> 
> When I compile it, I get:
> 
> Main.hs:130:5: Warning:
>      Pattern match(es) are overlapped
>      In a case alternative:
>          wxID_NO -> ...
>          wxID_YES -> ...
> 
> which is strange. I checked, wxID_NO is defined as 5104 and wxID_YES as 
> 5103. So they are not overlapping. On the other hand, when I replace the 
> wxID_NO/YES/CANCEL with the actual values, the warning vanishes.
> 
> Any Idea what this could be?
> 
> Thanks!
> Nathan
> 



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

Message: 6
Date: Sun, 01 Sep 2013 01:50:03 +0200
From: Nathan H?sken <nathan.hues...@posteo.de>
To: "Edward Z. Yang" <ezy...@mit.edu>
Cc: Haskell Beginners Mailinglist <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Pattern match(es) are overlapped ...
        but I do not see that they do
Message-ID: <5222812b.4090...@posteo.de>
Content-Type: text/plain; charset=UTF-8; format=flowed

Hey,

Thanks for the reply. Ah, yes of course. I see the problem.

How exactly would you do the encoding into an ADT?
What I also could do is used nested ifs ...

Is there no easy, compact way to switch over a set of ints in haskell, 
that are defined in variables?

Best Regards,
Nathan

On 09/01/2013 01:12 AM, Edward Z. Yang wrote:
> Hello Nathan,
>
> The problem is that because these "numbers" are actually
> variable names, Haskell is not pattern matching against the
> number; it is actually binding the result type to the variable.
> You might as well have written:
>
>>       case res of
>>         x -> return False
>>         x -> return True
>>         x -> do
> which of course is overlapping.
>
> There are a few ways to rewrite your program, but my recommendation
> would be to define an abstract data type which encodes the three
> choices, and pattern match against that.
>
> Edward
>
> Excerpts from Nathan H?sken's message of Sat Aug 31 15:46:01 -0700 2013:
>> Hey,
>>
>> I have the following code (which uses wxHaskell):
>>
>> import Graphics.UI.WX
>> import Graphics.UI.WX.Controls
>> import Graphics.UI.WXCore
>> import Graphics.UI.WXCore.Events
>>
>> (...)
>>
>> dealWithUnsavedChanges :: Var ProgramState -> TextCtrl a -> IO Bool
>> dealWithUnsavedChanges state tc = do
>>     ProgramState unsavedChanges _ <- varGet state
>>     if not unsavedChanges then return True else do
>>       res <- messageDialog tc "Unsaved changes ..." "You have unsaved
>> changes, do you want to save them?" (wxYES_NO .+. wxCANCEL .+.
>> wxICON_EXCLAMATION)
>>       case res of
>>         wxID_CANCEL -> return False
>>         wxID_NO     -> return True
>>         wxID_YES    -> do
>>           onSave state tc
>>           -- check if saving worked
>>           ProgramState newUnsavedChanges _ <- varGet state
>>           return (not newUnsavedChanges)
>>
>> When I compile it, I get:
>>
>> Main.hs:130:5: Warning:
>>       Pattern match(es) are overlapped
>>       In a case alternative:
>>           wxID_NO -> ...
>>           wxID_YES -> ...
>>
>> which is strange. I checked, wxID_NO is defined as 5104 and wxID_YES as
>> 5103. So they are not overlapping. On the other hand, when I replace the
>> wxID_NO/YES/CANCEL with the actual values, the warning vanishes.
>>
>> Any Idea what this could be?
>>
>> Thanks!
>> Nathan
>>




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

Message: 7
Date: Sun, 1 Sep 2013 11:16:20 +1000
From: Tony Morris <tmor...@tmorris.net>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Early return in IO monad
Message-ID:
        <cajf6usiu+kjpvh4l33nehyzmzpvelgg3s2g7d+4fdy8nxm1...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

unless somePreCondition ...
 On 01/09/2013 4:07 AM, "Nathan H?sken" <nathan.hues...@posteo.de> wrote:

> Hey,
>
> Is it somehow possible to return "early" in a do block of the IO monad?
> The eqivalent do in C:
>
> void doBlock() {
>     if (some preCondition) {
>       return;
>     }
>     ...
> }
>
> ???
>
> Thanks!
> Nathan
>
> ______________________________**_________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/**mailman/listinfo/beginners<http://www.haskell.org/mailman/listinfo/beginners>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130901/f0d523c4/attachment-0001.html>

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

Message: 8
Date: Sat, 31 Aug 2013 20:07:59 -0700
From: "Edward Z. Yang" <ezy...@mit.edu>
To: Nathan H?sken <nathan.hues...@posteo.de>
Cc: Haskell Beginners Mailinglist <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Pattern match(es) are overlapped ...
        but I   do not see that they do
Message-ID: <1378004547-sup-7794@javelin>
Content-Type: text/plain; charset=UTF-8

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.

Another mechanism is to define a map (e.g. Data.Map or Data.IntMap)
mapping integers to the appropriate values, and then do a table lookup.

Edward

Excerpts from Nathan H?sken's message of Sat Aug 31 16:50:03 -0700 2013:
> Hey,
> 
> Thanks for the reply. Ah, yes of course. I see the problem.
> 
> How exactly would you do the encoding into an ADT?
> What I also could do is used nested ifs ...
> 
> Is there no easy, compact way to switch over a set of ints in haskell, 
> that are defined in variables?
> 
> Best Regards,
> Nathan
> 
> On 09/01/2013 01:12 AM, Edward Z. Yang wrote:
> > Hello Nathan,
> >
> > The problem is that because these "numbers" are actually
> > variable names, Haskell is not pattern matching against the
> > number; it is actually binding the result type to the variable.
> > You might as well have written:
> >
> >>       case res of
> >>         x -> return False
> >>         x -> return True
> >>         x -> do
> > which of course is overlapping.
> >
> > There are a few ways to rewrite your program, but my recommendation
> > would be to define an abstract data type which encodes the three
> > choices, and pattern match against that.
> >
> > Edward
> >
> > Excerpts from Nathan H?sken's message of Sat Aug 31 15:46:01 -0700 2013:
> >> Hey,
> >>
> >> I have the following code (which uses wxHaskell):
> >>
> >> import Graphics.UI.WX
> >> import Graphics.UI.WX.Controls
> >> import Graphics.UI.WXCore
> >> import Graphics.UI.WXCore.Events
> >>
> >> (...)
> >>
> >> dealWithUnsavedChanges :: Var ProgramState -> TextCtrl a -> IO Bool
> >> dealWithUnsavedChanges state tc = do
> >>     ProgramState unsavedChanges _ <- varGet state
> >>     if not unsavedChanges then return True else do
> >>       res <- messageDialog tc "Unsaved changes ..." "You have unsaved
> >> changes, do you want to save them?" (wxYES_NO .+. wxCANCEL .+.
> >> wxICON_EXCLAMATION)
> >>       case res of
> >>         wxID_CANCEL -> return False
> >>         wxID_NO     -> return True
> >>         wxID_YES    -> do
> >>           onSave state tc
> >>           -- check if saving worked
> >>           ProgramState newUnsavedChanges _ <- varGet state
> >>           return (not newUnsavedChanges)
> >>
> >> When I compile it, I get:
> >>
> >> Main.hs:130:5: Warning:
> >>       Pattern match(es) are overlapped
> >>       In a case alternative:
> >>           wxID_NO -> ...
> >>           wxID_YES -> ...
> >>
> >> which is strange. I checked, wxID_NO is defined as 5104 and wxID_YES as
> >> 5103. So they are not overlapping. On the other hand, when I replace the
> >> wxID_NO/YES/CANCEL with the actual values, the warning vanishes.
> >>
> >> Any Idea what this could be?
> >>
> >> Thanks!
> >> Nathan
> >>



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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


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

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

Reply via email to