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

Daniel Fischer daniel.is.fischer at web.de
Fri May 4 14:03:14 EDT 2007


> -----Ursprüngliche Nachricht-----
> Von: "Rich Neswold" <rich.neswold at gmail.com>
> Gesendet: 04.05.07 19:31:53
> An: Phlex <Phlex at telenet.be>
> CC: haskell-cafe at haskell.org
> Betreff: Re: [Haskell-cafe] [IO Int] -> IO [Int]

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]
> 
> 
> Control.Monad has a function (called "sequence") that does this for you. In fact, "sequence" is a more generic solution since its signature is (
> Monad m => [m a] -> m [a]).

Unfortunately, this won't work for infinite lists either, for those you'd need an 'unsafe' action, namely 'unsafeInterleaveIO', like


import System.IO.Unsafe

conv :: [IO a] -> IO [a]
conv [] = return []
conv (x:xs) = do a <- x
                 as <- unsafeInterleaveIO (conv xs)
                 return (a:as)


*Test> :set -fno-print-bind-result
*Test> xs <- conv $ map return [1 .. ]
*Test> take 20 xs
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

> 
> As a newbie, I found it educational to peruse the various modules in "http://haskell.org/ghc/docs/latest/html/libraries/
> ". Just start looking at modules that sound interesting and see what has already been defined. Some modules at first may be too advanced, but if you go back to them in a few days (weeks?), they'll start making more sense, too.
> 

Very good advice, I think, and it's also very instructive to read the source code, you can learn a lot from that.

> 
> -- 
> Rich

HTH,
Daniel




More information about the Haskell-Cafe mailing list