[Haskell-cafe] Re: Nested unsafePerformIO?

2010-04-08 Thread Ertugrul Soeylemez
DavidA  wrote:

> I am having difficulty debugging a troublesome stack overflow, which I
> think might be related to calling unsafePerformIO from within the IO
> monad.
>
> [...]
>
> f x = unsafePerformIO $ do
> m <- randomRIO (1,2)
> return (m+x)

As a side note you don't need unsafePerformIO here.  Instead you should
implement something like iterateM:

  iterateM :: Monad m => (a -> m a) -> a -> m [a]

or change the type of your timedIterateIO to:

  timedIterateIO :: Int -> (a -> IO a) -> a -> IO a

Also to do the actual timing you can use concurrency with SampleVar,
which is cleaner and probably also faster:

  timedIterateIO :: Int -> (a -> IO a) -> a -> IO a
  timedIterateIO time f x0 = do
resultVar <- newSampleVar x0
tid <- forkIO $ iterateFunc resultVar x0
threadDelay time
readSampleVar resultVar <* killThread tid

where
  iterateFunc resultVar x0 = x0 `seq` do
x1 <- f x0
writeSampleVar resultVar x1
iterateFunc resultVar x1


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/


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


[Haskell-cafe] Re: Nested unsafePerformIO?

2010-04-08 Thread DavidA
Bas van Dijk  gmail.com> writes:

> 
> It looks like your timedIterateIO is too lazy.
> 
> Try evaluating the 'y' before calling timedIterateIO' again as in:
> 
> let y = f x
> ... y `seq` timedIterateIO' t0 y
> 

Thank you,

that appears to do the trick.

I'm still a bit puzzled about why the problem only manifested in this case
- it didn't manifest when I used iterate, for example.


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