
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

On 5/4/07, Phlex
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 (Monadhttp://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html#t%...m => [m a] -> m [a]). 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. -- Rich AIM : rnezzy ICQ : 174908475 Jabber: rich@neswold.homeunix.net

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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hello,
I'm trying to learn haskell, so here's is my first newbie question. I hope this list is appropriate for such help requests.
Yes.
I'm trying to write a function with the signature [IO Int] -> IO [Int]
As other people have mentioned, the library function sequence has this type (actually a type which generalizes this type).
conv2 :: [IO Int] -> IO [Int] conv2 l = do val <- (head l) rest <- (conv2 (tail l)) return (val : rest)
That works,
This doesn't quite work since you don't cover the case for empty lists. You need to add another clause: conv2 [] = return []
but it won't work for infinite lists. How could I achieve the desired result ?
I don't think a function of this type makes sense for infinite lists. Even the library function sequence will diverge on an infinite list. The issue is that you must evaluate all of the input [IO Int] to get the pure [Int] output. In other words, the type IO [Int] means an IO action which results in a (pure) list of ints. Thus, you cannot start returning the result [Int] while there are still IO actions to perform to compute the rest of the result. -Jeff --- This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Hoogle is also helpful
http://haskell.org/hoogle/?q=%5Bm+a%5D+-%3E+m+%5Ba%5D
2007/5/4, Phlex
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Fri, May 04, 2007 at 19:23:16 +0200, Phlex 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 ?
I had a similar problem a little while back. I also asked on this list and maybe that discussion[1] can illuminate the issue. I also put an entry on my blog afterwards in an attempt to capture my understanding of it all[2]. /M [1]: http://www.haskell.org/pipermail/haskell-cafe/2007-January/021367.html [2]: http://therning.org/magnus/archives/249 -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus@therning.org Jabber: magnus.therning@gmail.com http://therning.org/magnus
participants (6)
-
Alfonso Acosta
-
Jeff Polakow
-
Magnus Therning
-
Phlex
-
Rich Neswold
-
Thomas Hartman