Question

The error:

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 }'

And the code:

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"

If anyone can help me with the MaybeT transformer, I'd appreciate it. I can't figure out how to get this working.

Was it helpful?

Solution

There's no benefit here to using MaybeT in this little example: you could write the three lines using MaybeT as:

Data.Foldable.traverse_  putStrLn $ M.lookup "a" a

There's no automatic conversion of Maybe to MaybeT (though there might be if Maybe was actually defined as MaybeT Identity), so you have to apply a function to the result of M.lookup like:

(MaybeT . return) :: Maybe a -> MaybeT m a
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top