Good way of combining functions of type IO (Maybe a)

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 c func = 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

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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

I'm a beginner as well, and I explored how to use the maybeT transformer
and wrote it up on my blog across a few posts. Mostly they were just for my
own learning purposes, but maybe you can find something in them too?
http://watchchrislearn.com/blog/2013/11/28/playing-with-the-either-monad/
http://watchchrislearn.com/blog/2013/11/30/using-the-either-monad-inside-ano...
http://watchchrislearn.com/blog/2013/11/30/eithert-inside-of-io/
http://watchchrislearn.com/blog/2013/12/01/working-entirely-in-eithert/
On Mon, Mar 3, 2014 at 8:21 AM, David McBride
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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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

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
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 Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (4)
-
Chris Schneider
-
David McBride
-
Nathan Hüsken
-
Yitzchak Gale