[Haskell-beginners] Maybe monad and computations

Emmanuel Touzery etouzery at gmail.com
Fri Sep 6 22:43:48 CEST 2013


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" <nick.vanderweit at gmail.com>
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 <mikesteele81 at gmail.com>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 <etouzery at gmail.com>
>> 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
>> > Beginners at haskell.org
>> > http://www.haskell.org/mailman/listinfo/beginners
>> >
>>
>>
>>
>> --
>> -- Michael Steele
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> 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.htm>


More information about the Beginners mailing list