[Haskell-cafe] [IO Int] -> IO [Int]

Alfonso Acosta alfonso.acosta at gmail.com
Fri May 4 13:35:07 EDT 2007


the 'sequence' prelude function does exactly what you want.


It's implemented as

sequence ms = foldr k (return []) ms
	    where
	      k m m' = do { x <- m; xs <- m'; return (x:xs) }

On 5/4/07, Phlex <Phlex at telenet.be> wrote:
> Hello all,
>
> I'm trying to learn haskell, so here's is my first newbie question.
> I hope this list is appropriate for such help requests.
>
> I'm trying to write a function with the signature [IO Int] -> IO [Int]
>
> Here is my first attempt :
>
> conv :: [IO Int] -> IO [Int]
> conv l = do val <- (head l)
>             return (val : (conv (tail l)))
>
> This does not work as I'm consing an Int to an (IO [Int]).
> So I tried this :
>
> conv2 :: [IO Int] -> IO [Int]
> conv2 l = do val <- (head l)
>              rest <- (conv2 (tail l))
>              return (val : rest)
>
> That works, but it won't work for infinite lists.
> How could I achieve the desired result ?
>
> Thanks in advance,
> Sacha
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list