[Haskell-cafe] [IO Int] -> IO [Int]
Jeff Polakow
jeff.polakow at db.com
Fri May 4 13:50:21 EDT 2007
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070504/6e6d007f/attachment.htm
More information about the Haskell-Cafe
mailing list