Re: [Haskell-cafe] infinite list of random elements

2007-07-31 Thread Lennart Augustsson
No leak in sight. -- Lennart import Random import Array randomElts :: RandomGen g = g - [a] - [a] randomElts _ [] = [] randomElts g xs = map (a!) rs where a = listArray (1, n) xs rs = randomRs (1, n) g n = length xs main = do g - getStdGen let xs = randomElts g

Re: [Haskell-cafe] infinite list of random elements

2007-07-31 Thread Chad Scherrer
Ok, that looks good, but what if I need some random values elsewhere in the program? This doesn't return a new generator (and it can't because you never get to the end of the list). Without using IO or ST, you'd have to thread the parameter by hand or use the State monad, right? This is where I

Re: [Haskell-cafe] infinite list of random elements

2007-07-31 Thread Brandon S. Allbery KF8NH
On Jul 31, 2007, at 16:20 , Chad Scherrer wrote: calls. I suppose a State' monad, strict in the state, could help here. You mean Control.Monad.State.Strict ? -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [EMAIL PROTECTED] system administrator [openafs,heimdal,too many hats]

Re: [Haskell-cafe] infinite list of random elements

2007-07-31 Thread Chad Scherrer
On 7/31/07, Brandon S. Allbery KF8NH [EMAIL PROTECTED] wrote: On Jul 31, 2007, at 16:20 , Chad Scherrer wrote: calls. I suppose a State' monad, strict in the state, could help here. You mean Control.Monad.State.Strict ? Umm, yeah, I guess I do. Glad I hadn't started recoding it!

[Haskell-cafe] infinite list of random elements

2007-07-30 Thread Chad Scherrer
I'm trying to do something I thought would be pretty simple, but it's giving me trouble. Given a list, say [1,2,3], I'd like to be able to generate an infinite list of random elements from that list, in this case maybe [1,2,1,3,2,1,3,2,3,1,2,...]. I'm using IO for random purely due to laziness

Re: [Haskell-cafe] infinite list of random elements

2007-07-30 Thread Stefan O'Rear
On Mon, Jul 30, 2007 at 02:40:35PM -0700, Chad Scherrer wrote: I'm trying to do something I thought would be pretty simple, but it's giving me trouble. Given a list, say [1,2,3], I'd like to be able to generate an infinite list of random elements from that list, in this case maybe

Re: [Haskell-cafe] infinite list of random elements

2007-07-30 Thread Cale Gibbard
On 30/07/07, Chad Scherrer [EMAIL PROTECTED] wrote: I'm trying to do something I thought would be pretty simple, but it's giving me trouble. Given a list, say [1,2,3], I'd like to be able to generate an infinite list of random elements from that list, in this case maybe

Re: [Haskell-cafe] infinite list of random elements

2007-07-30 Thread Lauri Alanko
On Mon, Jul 30, 2007 at 02:40:35PM -0700, Chad Scherrer wrote: Given a list, say [1,2,3], I'd like to be able to generate an infinite list of random elements from that list, in this case maybe [1,2,1,3,2,1,3,2,3,1,2,...]. I'm using IO for random purely due to laziness (my own, not Haskell's).

Re: [Haskell-cafe] infinite list of random elements

2007-07-30 Thread Lennart Augustsson
Why this obsession with IO? There should be no IO involved in this, except for getting the initial generator. Using IO just confuses what is going on. -- Lennart On 7/30/07, Cale Gibbard [EMAIL PROTECTED] wrote: On 30/07/07, Chad Scherrer [EMAIL PROTECTED] wrote: I'm trying to do

Re: [Haskell-cafe] infinite list of random elements

2007-07-30 Thread Sebastian Sylvan
On 30/07/07, Lennart Augustsson [EMAIL PROTECTED] wrote: Why this obsession with IO? There should be no IO involved in this, except for getting the initial generator. Using IO just confuses what is going on. Indeed. Here's my version: -- first define a shuffle function, completely pure!

Re: [Haskell-cafe] infinite list of random elements

2007-07-30 Thread Chad Scherrer
Thanks for your responses. Stefan, I appreciate your taking a step back for me (hard to judge what level of understanding someone is coming from), but the example you gave doesn't contradict my intuition either. I don't consider the output [IO a] a list of tainted a's, but, as you suggest, a list