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