错误:
maybet.hs:8:14:
Couldn't match expected type `MaybeT m0 t0'
with actual type `Maybe a0'
In the return type of a call of `M.lookup'
In a stmt of a 'do' expression: m <- M.lookup "a" a
In the second argument of `($)', namely
`do { m <- M.lookup "a" a;
lift $ putStrLn m }'
代码:
import qualified Data.Map as M
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Class
main = do
let a = M.fromList [("a", "b"), ("c", "d")]
runMaybeT $ do
m <- M.lookup "a" a
lift $ putStrLn m
putStrLn "done"
如果有人可以帮我使用MaybeT变压器,我会很感激。我无法弄清楚如何使这个工作。
答案 0 :(得分:2)
在这个小例子中使用MaybeT没有任何好处:你可以使用MaybeT将这三行写成:
Data.Foldable.traverse_ putStrLn $ M.lookup "a" a
没有自动将Maybe转换为MaybeT(虽然可能有如果Maybe实际上被定义为MaybeT Identity),所以你必须将函数应用于M.lookup的结果,如:
(MaybeT . return) :: Maybe a -> MaybeT m a