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:  Maybe monad and computations (divyanshu ranjan)
   2. Re:  Maybe monad and computations (Michael Steele)
   3. Re:  Maybe monad and computations (Nicholas Vanderweit)
   4. Re:  Maybe monad and computations (Emmanuel Touzery)
   5. Re:  Maybe monad and computations (Emmanuel Touzery)


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

Message: 1
Date: Sat, 7 Sep 2013 01:51:48 +0530
From: divyanshu ranjan <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Maybe monad and computations
Message-ID:
        <cal9hw252tyir4kpmkm9stxmkjkheepf+x_qo6ttm+czw7ym...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Hi,
  Can you provide little more details or code snippet as It is not
clear what you want to achieve.

Thanks
Divyanshu Ranjan

On Sat, Sep 7, 2013 at 1:19 AM, Emmanuel Touzery <[email protected]> wrote:
> Hello,
>
>  I'm often using the Maybe monad to combine Maybe computations one after the
> other without conditionals.
>
>  But I'm not sure how to do it, when I have several operations which return
> Maybe and I want all the indiviual values in the end, not a combination of
> the values.
>
>  Currently I do:
>
> let allTogether = do
>   ma <- doA
>   mb <- doB
>   return (ma, mb)
> case allTogether of
>   Nothing -> ...
>   Just (a, b) -> ...
>
>  This way in this case I have one case instead of two, however I'm sure
> there must be a nicer way to achieve this result?
>
>  Thank you!
>
> Emmanuel
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



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

Message: 2
Date: Fri, 6 Sep 2013 13:28:04 -0700
From: Michael Steele <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Maybe monad and computations
Message-ID:
        <CAAFFR7A=zev6a8yqf9b7tai3eyq1_6kruqrjuuhqoz+r-vq...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

The `sequence`, `mapM`, and `mapM_` functions may be what you are looking for.

let allTogether = sequence [doA, doB, doC, doZ]
case allTogether of
  Nothing -> ...
  Just as -> ...


Use `maybe` and `fromMaybe` to avoid case statements:

fromMaybe "failure!" $ do
    as <- sequence [doA, doB, doC, doZ]
    return $ "Success: " ++ show (length as)

The `catMaybes` function can be used to execute each computation in
the list even if some of them return nothing.

On Fri, Sep 6, 2013 at 12:49 PM, Emmanuel Touzery <[email protected]> wrote:
> Hello,
>
>  I'm often using the Maybe monad to combine Maybe computations one after the
> other without conditionals.
>
>  But I'm not sure how to do it, when I have several operations which return
> Maybe and I want all the indiviual values in the end, not a combination of
> the values.
>
>  Currently I do:
>
> let allTogether = do
>   ma <- doA
>   mb <- doB
>   return (ma, mb)
> case allTogether of
>   Nothing -> ...
>   Just (a, b) -> ...
>
>  This way in this case I have one case instead of two, however I'm sure
> there must be a nicer way to achieve this result?
>
>  Thank you!
>
> Emmanuel
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
-- Michael Steele



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

Message: 3
Date: Fri, 6 Sep 2013 14:35:58 -0600
From: Nicholas Vanderweit <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Maybe monad and computations
Message-ID:
        <can0ny8y1tib6zhblvztrbu+2igeqova6mujc_qgk6quvjbk...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

It seems like he wants something like sequence, but for tuples. Say:

sequence2 :: Monad m => (m a, m b) -> m (a, b)
sequence2 (ma, mb) = ma >>= \a -> mb >>= \b -> return (a, b)

But without knowing the use case it's hard to know whether or not this
could be done simply with "sequence" and mapM*


Nick


On Fri, Sep 6, 2013 at 2:28 PM, Michael Steele <[email protected]>wrote:

> The `sequence`, `mapM`, and `mapM_` functions may be what you are looking
> for.
>
> let allTogether = sequence [doA, doB, doC, doZ]
> case allTogether of
>   Nothing -> ...
>   Just as -> ...
>
>
> Use `maybe` and `fromMaybe` to avoid case statements:
>
> fromMaybe "failure!" $ do
>     as <- sequence [doA, doB, doC, doZ]
>     return $ "Success: " ++ show (length as)
>
> The `catMaybes` function can be used to execute each computation in
> the list even if some of them return nothing.
>
> On Fri, Sep 6, 2013 at 12:49 PM, Emmanuel Touzery <[email protected]>
> wrote:
> > Hello,
> >
> >  I'm often using the Maybe monad to combine Maybe computations one after
> the
> > other without conditionals.
> >
> >  But I'm not sure how to do it, when I have several operations which
> return
> > Maybe and I want all the indiviual values in the end, not a combination
> of
> > the values.
> >
> >  Currently I do:
> >
> > let allTogether = do
> >   ma <- doA
> >   mb <- doB
> >   return (ma, mb)
> > case allTogether of
> >   Nothing -> ...
> >   Just (a, b) -> ...
> >
> >  This way in this case I have one case instead of two, however I'm sure
> > there must be a nicer way to achieve this result?
> >
> >  Thank you!
> >
> > Emmanuel
> >
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> >
>
>
>
> --
> -- Michael Steele
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130906/4eb0aa24/attachment-0001.html>

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

Message: 4
Date: Fri, 6 Sep 2013 22:43:48 +0200
From: Emmanuel Touzery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Maybe monad and computations
Message-ID:
        <cac42rekytt2auxnuft_yp+psy-b7r2xohnws4g087u3sfh4...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Thank you (and the others) for tips. I'll check the suggestions but yes
sequence requires that all items have the same type so that sequence2 would
work better but feels un-idiomatic.
I needed such code several times in several contexts, so i assumed the
answer would be obvious. Maybe i'm structuring my code the wrong way for
haskell, i'll rethink those functions. Thanks for now!

Emmanuel
On Sep 6, 2013 10:37 PM, "Nicholas Vanderweit" <[email protected]>
wrote:

> It seems like he wants something like sequence, but for tuples. Say:
>
> sequence2 :: Monad m => (m a, m b) -> m (a, b)
> sequence2 (ma, mb) = ma >>= \a -> mb >>= \b -> return (a, b)
>
> But without knowing the use case it's hard to know whether or not this
> could be done simply with "sequence" and mapM*
>
>
> Nick
>
>
> On Fri, Sep 6, 2013 at 2:28 PM, Michael Steele <[email protected]>wrote:
>
>> The `sequence`, `mapM`, and `mapM_` functions may be what you are looking
>> for.
>>
>> let allTogether = sequence [doA, doB, doC, doZ]
>> case allTogether of
>>   Nothing -> ...
>>   Just as -> ...
>>
>>
>> Use `maybe` and `fromMaybe` to avoid case statements:
>>
>> fromMaybe "failure!" $ do
>>     as <- sequence [doA, doB, doC, doZ]
>>     return $ "Success: " ++ show (length as)
>>
>> The `catMaybes` function can be used to execute each computation in
>> the list even if some of them return nothing.
>>
>> On Fri, Sep 6, 2013 at 12:49 PM, Emmanuel Touzery <[email protected]>
>> wrote:
>> > Hello,
>> >
>> >  I'm often using the Maybe monad to combine Maybe computations one
>> after the
>> > other without conditionals.
>> >
>> >  But I'm not sure how to do it, when I have several operations which
>> return
>> > Maybe and I want all the indiviual values in the end, not a combination
>> of
>> > the values.
>> >
>> >  Currently I do:
>> >
>> > let allTogether = do
>> >   ma <- doA
>> >   mb <- doB
>> >   return (ma, mb)
>> > case allTogether of
>> >   Nothing -> ...
>> >   Just (a, b) -> ...
>> >
>> >  This way in this case I have one case instead of two, however I'm sure
>> > there must be a nicer way to achieve this result?
>> >
>> >  Thank you!
>> >
>> > Emmanuel
>> >
>> > _______________________________________________
>> > Beginners mailing list
>> > [email protected]
>> > http://www.haskell.org/mailman/listinfo/beginners
>> >
>>
>>
>>
>> --
>> -- Michael Steele
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130906/53e924cf/attachment-0001.html>

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

Message: 5
Date: Fri, 6 Sep 2013 23:04:46 +0200
From: Emmanuel Touzery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Maybe monad and computations
Message-ID:
        <CAC42RemCY=wGAMgVLPCyeu7_ECjLVrf1uOU04fev9=8sfe2...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hmm I've been looking some more. Turns out I'm pretty sure... What I've
been looking for is liftMx.

In this case, liftM2.

liftM2 ... doA doB

where "..." represents the rest of the computation as it did in the example
snippet I sent in my first email.

In the case of the Maybe monad, "..." will be called with the "Just" values
and only if both doA and doB are not Nothing.

and for "n" operations, there is "ap", I'll look at that one too.

emmanuel


On Fri, Sep 6, 2013 at 10:43 PM, Emmanuel Touzery <[email protected]>wrote:

> Thank you (and the others) for tips. I'll check the suggestions but yes
> sequence requires that all items have the same type so that sequence2 would
> work better but feels un-idiomatic.
> I needed such code several times in several contexts, so i assumed the
> answer would be obvious. Maybe i'm structuring my code the wrong way for
> haskell, i'll rethink those functions. Thanks for now!
>
> Emmanuel
> On Sep 6, 2013 10:37 PM, "Nicholas Vanderweit" <[email protected]>
> wrote:
>
>> It seems like he wants something like sequence, but for tuples. Say:
>>
>> sequence2 :: Monad m => (m a, m b) -> m (a, b)
>> sequence2 (ma, mb) = ma >>= \a -> mb >>= \b -> return (a, b)
>>
>> But without knowing the use case it's hard to know whether or not this
>> could be done simply with "sequence" and mapM*
>>
>>
>> Nick
>>
>>
>> On Fri, Sep 6, 2013 at 2:28 PM, Michael Steele <[email protected]>wrote:
>>
>>> The `sequence`, `mapM`, and `mapM_` functions may be what you are
>>> looking for.
>>>
>>> let allTogether = sequence [doA, doB, doC, doZ]
>>> case allTogether of
>>>   Nothing -> ...
>>>   Just as -> ...
>>>
>>>
>>> Use `maybe` and `fromMaybe` to avoid case statements:
>>>
>>> fromMaybe "failure!" $ do
>>>     as <- sequence [doA, doB, doC, doZ]
>>>     return $ "Success: " ++ show (length as)
>>>
>>> The `catMaybes` function can be used to execute each computation in
>>> the list even if some of them return nothing.
>>>
>>> On Fri, Sep 6, 2013 at 12:49 PM, Emmanuel Touzery <[email protected]>
>>> wrote:
>>> > Hello,
>>> >
>>> >  I'm often using the Maybe monad to combine Maybe computations one
>>> after the
>>> > other without conditionals.
>>> >
>>> >  But I'm not sure how to do it, when I have several operations which
>>> return
>>> > Maybe and I want all the indiviual values in the end, not a
>>> combination of
>>> > the values.
>>> >
>>> >  Currently I do:
>>> >
>>> > let allTogether = do
>>> >   ma <- doA
>>> >   mb <- doB
>>> >   return (ma, mb)
>>> > case allTogether of
>>> >   Nothing -> ...
>>> >   Just (a, b) -> ...
>>> >
>>> >  This way in this case I have one case instead of two, however I'm sure
>>> > there must be a nicer way to achieve this result?
>>> >
>>> >  Thank you!
>>> >
>>> > Emmanuel
>>> >
>>> > _______________________________________________
>>> > Beginners mailing list
>>> > [email protected]
>>> > http://www.haskell.org/mailman/listinfo/beginners
>>> >
>>>
>>>
>>>
>>> --
>>> -- Michael Steele
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130906/171564fb/attachment.html>

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

Subject: Digest Footer

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


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

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

Reply via email to