0

错误:

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 变压器,我将不胜感激。我不知道如何让这个工作。

4

1 回答 1

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
于 2013-01-05T06:11:19.323 回答