[Haskell-cafe] Re: Nested unsafePerformIO?

Ertugrul Soeylemez es at ertes.de
Fri Apr 9 00:47:19 EDT 2010


DavidA <polyomino at f2s.com> 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/




More information about the Haskell-Cafe mailing list