Re: [Haskell-cafe] [IO Int] - IO [Int]

2007-05-06 Thread Magnus Therning
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)
[EMAIL PROTECTED] Jabber: [EMAIL PROTECTED]
http://therning.org/magnus


pgpWXpCLxe8LB.pgp
Description: PGP signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] [IO Int] - IO [Int]

2007-05-05 Thread Thomas Hartman

Hoogle is also helpful

http://haskell.org/hoogle/?q=%5Bm+a%5D+-%3E+m+%5Ba%5D

2007/5/4, Phlex [EMAIL PROTECTED]:

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


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2007-05-04 Thread 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


Re: [Haskell-cafe] [IO Int] - IO [Int]

2007-05-04 Thread Rich Neswold

On 5/4/07, Phlex [EMAIL PROTECTED] 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
(Monadhttp://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html#t%3AMonadm
= [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: [EMAIL PROTECTED]
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] [IO Int] - IO [Int]

2007-05-04 Thread Jeff Polakow
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.___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] [IO Int] - IO [Int]

2007-05-04 Thread Daniel Fischer

 -Ursprüngliche Nachricht-
 Von: Rich Neswold [EMAIL PROTECTED]
 Gesendet: 04.05.07 19:31:53
 An: Phlex [EMAIL PROTECTED]
 CC: haskell-cafe@haskell.org
 Betreff: Re: [Haskell-cafe] [IO Int] - IO [Int]

On 5/4/07, Phlex [EMAIL PROTECTED] 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


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] [IO Int] - IO [Int]

2007-05-04 Thread Phlex

Daniel Fischer wrote:

On 5/4/07, Phlex [EMAIL PROTECTED] wrote:
  

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]
  
HTH,

Daniel
  

Thank you all for the very good answers.

Sacha
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe