How to simplify the code of Maybe within a monad?

Hi, Since Maybe is a monad, I could write code like 'maybeA >> maybeB >> maybeC' to check if all these are not Nothing. Or 'liftM foo maybeD' to avoid ugly 'case of'. But how if here maybe[ABC] are like 'IO (Maybe Int)', or foo is type of 'Int -> IO Int'? -- 竹密岂妨流水过 山高哪阻野云飞 And for G+, please use magiclouds#gmail.com.

Magicloud Magiclouds
Since Maybe is a monad, I could write code like 'maybeA >> maybeB >> maybeC' to check if all these are not Nothing. Or 'liftM foo maybeD' to avoid ugly 'case of'.
Also check out the somewhat cleaner Functor class with its liftM equivalent called 'fmap', for which you don't need to import Control.Monad. For monads fmap = liftM.
But how if here maybe[ABC] are like 'IO (Maybe Int)', or foo is type of 'Int -> IO Int'?
Well, this is Haskell, so you can always write your own higher order functions: (~>>=) :: (Monad m) => m (Maybe a) -> (a -> m (Maybe b)) -> m (Maybe b) c ~>>= f = c >>= maybe (return Nothing) f (~>>) :: (Monad m) => m (Maybe a) -> m (Maybe b) -> m (Maybe b) c ~>> d = c >>= maybe (return Nothing) (const d) infixl 1 ~>>= infixl 1 ~>> However in the second case of course there is no Maybe, but then notice that IO itself acts like Maybe through its exceptions. In fact Maybe is a transparent exception monad. Greets, Ertugrul -- Not to be or to be and (not to be or to be and (not to be or to be and (not to be or to be and ... that is the list monad.

On 17 августа 2012 11:48:54 Magicloud Magiclouds wrote:
But how if here maybe[ABC] are like 'IO (Maybe Int)', or foo is type of 'Int -> IO Int'?
Maybe you can help monad transformers and MaybeT? They help you write code like 'MaybeT f1 >> MaybeT f2 >> MaybeT f3 where { f1, f2, f3 :: IO (Maybe Int) }'.
participants (3)
-
Ertugrul Söylemez
-
Magicloud Magiclouds
-
Тимофеев Никита Дмитриевич