[Haskell-beginners] Good way of combining functions of type IO (Maybe a)

David McBride toad3k at gmail.com
Mon Mar 3 15:21:03 UTC 2014


cabal install maybet

import Control.Monad.Maybe

f1 :: IO (Maybe Int)
f1 = return . Just $ 1

d2 :: Int -> IO (Maybe String)
d2 = return . Just . show

blah :: IO (Maybe (Int, String))
blah = do
  runMaybeT $ do
  a <- MaybeT f1
  b <- MaybeT $ d2 a
  return (a,b)

Or slightly rewritten:

f1 :: MaybeT IO Int
f1 = return 1
-- f1 = fail "why oh why?!?"

d2 :: Int -> MaybeT IO String
d2 = return . show

blah = do
  runMaybeT $ do
  a <- f1
  b <- d2 a
  return (a,b)



On Mon, Mar 3, 2014 at 10:10 AM, Nathan Hüsken <nathan.huesken at posteo.de>wrote:

>  Hey,
>
> I want to write a function, which is basically a concatenation of
> functions of type "IO (Maybe a)".
> When they all where of type "Maybe a", no Problem I would simple use the
> Maybe monad.
>
> func :: Maybe cfunc = do
>   a <- f1
>   b <- d2 a
>   ...
>
> but now they are of type "IO (Maybe a)". Is there some way of combing
> these in a similar smart way?
>
> Thanks!
> Nathan
>
> _______________________________________________
> 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/20140303/4ccbdf15/attachment.html>


More information about the Beginners mailing list