[Haskell-cafe] Monad transformer, liftIO
Creighton Hogg
wchogg at gmail.com
Fri Apr 3 13:16:57 EDT 2009
On Fri, Apr 3, 2009 at 11:49 AM, Michael Roth <mroth at nessie.de> wrote:
> Hello list,
>
> maybe I'm just stupid, I'm trying to do something like this:
>
>
> import Control.Monad
> import Control.Monad.Trans
> import Control.Monad.List
>
> foobar = do
> a <- [1,2,3]
> b <- [4,5,6]
> liftIO $ putStrLn $ (show a) ++ " " ++ (show b)
> return (a+b)
>
> main = do
> sums <- foobar
> print sums
>
>
> But this apparently doesn't work... I'm total clueless how
> to achieve the correct solution. Maybe my mental image
> on the monad transformer thing is totally wrong?
Okay, so I think what you want is
import Control.Monad
import Control.Monad.Trans
import Control.Monad.List
foobar :: ListT IO Int
foobar = do
a <- msum . map return $ [1,2,3]
b <- msum . map return $ [4,5,6]
liftIO $ putStrLn $ (show a) ++ " " ++ (show b)
return (a+b)
main = do
sums <- runListT foobar
print sums
There were a couple of things going on here: first, that you tried to
use literal list syntax in do notation which I believe only works in
the actual [] monad. Second, you didn't have the runListT acting on
the foobar, which is how you go from a ListT IO Int to a IO [Int].
More information about the Haskell-Cafe
mailing list