Replace forever with explicit recursion.
foo :: Int -> IO () foo n = do use n foo (n+1)
Of course, you can use any type for your state instead of Int .
Otherwise, if you really want a variable state:
foo :: IO () foo = do r <- newIORef (0 :: Int) forever $ do n <- readIORef r use n writeIORef r (n+1)
If you really need variability for some other reason, I would not recommend the second option.
Adapting the specified idea to a specific code:
saveThreadFunc :: Int -> TQueue String -> IO () saveThreadFunc lastBackupTime queue = do newLine <- atomically $ readTQueue queue now <- round `fmap` getPOSIXTime :: IO Int writeFile "content.txt" newLine let makeNewBackup = now >= lastBackupTime + 86400 if makeNewBackup then do backupContent now newLine saveThreadFunc now queue else saveThreadFunc lastBackupTime queue
chi
source share