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:  Good way of combining functions of type IO (Maybe a)
      (David McBride)


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

Message: 1
Date: Tue, 4 Mar 2014 10:21:24 -0500
From: David McBride <[email protected]>
To: [email protected],  The Haskell-Beginners Mailing List - Discussion
        of primarily beginner-level topics related to Haskell
        <[email protected]>
Subject: Re: [Haskell-beginners] Good way of combining functions of
        type IO (Maybe a)
Message-ID:
        <can+tr43sboq59phffv5i1-wcacqedvxponvmvcp+g_w0dqb...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Huh I guess I've been using maybet for so long I never realized it had been
added to the transformers library.  TIL.


On Tue, Mar 4, 2014 at 2:13 AM, Yitzchak Gale <[email protected]> wrote:

> Nathan H?sken wrote:
> >> I want to write a function, which is basically a concatenation of
> >> functions of type "IO (Maybe a)".
>
> David McBride wrote:
> > cabal install maybet
>
> I would recommend using the standard MaybeT
> type, from the transformers library that is
> part of the Haskell Platform, unless you have
> a special reason to use a different library
> from Hackage.
>
> David's code works fine with the transformers library,
> after a slight modification of the import:
>
> import Control.Monad.Trans.Maybe
>
> Also - using the MaybeT monad is not the only
> way to do this. Maybe comes with a whole
> collection of very convenient combinators like
> "maybe", "fromMaybe", etc. So you can also do
> the calculation entirely in just IO without
> very much extra noice. Choose the approach
> that works out nicest for your particular
> application.
>
> Here is David's code, modified to do the
> calculations directly in the IO monad:
>
> f1 :: IO (Maybe Int)
> f1 = return . Just $ 1
>
> d2 :: Int -> IO (Maybe String)
> d2 = return . Just . show
>
> blah :: IO (Maybe (Int, String))
> blah = do
>   a <- f1
>   b <- maybe (return Nothing) d2 a
>   return (a,b)
>
> If you end up using things like
> "maybe (return Nothing)" and
> "fromMaybe (return Nothing)" a lot,
> you can define aliases for them.
> Perhaps there ought to be aliases like
> that in the standard libraries somewhere...
>
> Regards,
> Yitz
> _______________________________________________
> 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/20140304/8c2bb2b4/attachment-0001.html>

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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 69, Issue 10
*****************************************

Reply via email to