Monad变形金刚monad复制

时间:2013-05-19 17:08:29

标签: haskell io monads monad-transformers maybe

我是monad变形金刚的新手,很抱歉这个问题很简单。 我有值val :: MaybeT IO String和函数fn :: String -> IO [String]。 所以绑定后,我有val >>= liftM fn :: MaybeT IO (IO [String])。如何删除重复的IO monad并获得类型为MaybeT IO [String]的结果?

1 个答案:

答案 0 :(得分:13)

使用lift(或liftIO)代替liftM

> :t val >>= lift . fn
val >>= lift . fn :: MaybeT IO [String]

liftM用于在monad中应用纯函数。 liftliftIO用于将操作提升到变压器中。

liftM  :: Monad m => (a -> b) -> m a -> m b
lift   :: (Monad m, MonadTrans t) => m a -> t m a
liftIO :: MonadIO m => IO a -> m a