
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? Michael

You haven't really said what happens when you try this, but I would bet that
things would be clarified greatly if you put type signatures on your two
definitions.
On Fri, Apr 3, 2009 at 12:49 PM, Michael Roth
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?
Michael _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Fri, Apr 3, 2009 at 11:49 AM, Michael Roth
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].

Creighton Hogg schrieb:
Okay, so I think what you want is
[...]
Yes. Your solution works. Thank you. But:
a <- msum . map return $ [1,2,3]
Why Do I need this "msum . map return" thing? The "map return" part is somewhat clear. But not entirely. Which type of monad is created here? The msum-part ist totally confusing me: First we create a list with some monads (?) and then msum them? What is going on there?
first, that you tried to use literal list syntax in do notation which I believe only works in the actual [] monad.
Is the "x <- xs" in the list monad a form of syntactic sugar?
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].
Ah, yes. This point I understood now. Thank you again. Michael

Michael Roth wrote:
Hello list,
maybe I'm just stupid, I'm trying to do something like this:
Ciao Michael, As an alternative solution to Creighton's: import Control.Monad.List foobar :: ListT IO Int foobar = do a <- ListT . return $ [1,2,3] b <- ListT . return $ [4,5,6] liftIO $ putStrLn $ (show a) ++ " " ++ (show b) return (a+b) main = do sums <- runListT foobar print sums For the expression ListT . return $ [1,2,3] of type ListT IO Int, the return inferred is: return: a -> IO a return [1,2,3] = IO [1,2,3] and then you wrap the IO [] with the ListT newtype constructor. Paolo
participants (4)
-
Andrew Wagner
-
Creighton Hogg
-
Michael Roth
-
Paolo Losi