import Data.IORef runClosure f = mapM_ f [0..5] main = do x <- newIORef (0, (+ 1)) runClosure $ \i -> do (n,f) <- readIORef x print $ f n writeIORef x (f n, if i `mod` 2 == 0 then (+ 1) else (* 2))