[Haskell-cafe] can someone explain monad transformers to me, or how do you combine maybe and IO?

Anatoly Yakovenko aeyakovenko at gmail.com
Mon Nov 12 18:59:39 EST 2007


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?


More information about the Haskell-Cafe mailing list