can someone explain monad transformers to me, or how do you combine maybe and IO?

I wanted something that would work like liftM but with IO as well, so something like this:
liftM ((+) 1) $ Just 1 Just 2
but with the function lifted being of type (a -> IO b). so I came up with maybeIO::(a -> IO b) -> (Maybe a -> IO (Maybe b)) maybeIO ff = (\ aa -> case aa of Nothing -> return $ Nothing Just vv -> do rv <- ff vv return $ Just rv) incIO:: Int -> IO Int incIO ii = return $ ii + 1
maybeIO incIO $ Just 1 Just 2
works just like I want it to. But isn't this something that a monad transformer should be able to do?

On Nov 13, 2007 1:08 AM, Luke Palmer
We want MaybeT!
I third this proposal. It would be nice having MaybeT included in mtl. Besides, and although it's not exactly the same, you can "emulate" the Maybe monad by using the Either monad (the instance is defined in Control.Monad.Error). And in this case you want a monad transformer, so ErrorT can do the trick. Just consider that all the Left values are Nothing and the Right values are Just.
participants (3)
-
Alfonso Acosta
-
Anatoly Yakovenko
-
Luke Palmer