Skip to content

Instantly share code, notes, and snippets.

@jstimpfle
Created May 1, 2017 11:50
Show Gist options
  • Save jstimpfle/cb375a7c7d2fb0e840491cc6fe7c8407 to your computer and use it in GitHub Desktop.
Save jstimpfle/cb375a7c7d2fb0e840491cc6fe7c8407 to your computer and use it in GitHub Desktop.
-- Stateful closure in Haskell
import Data.IORef
makeCounter :: IO (Int -> IO Int)
makeCounter = do
state <- newIORef 0
return $ \i -> do
modifyIORef state (+i)
readIORef state
main = do
closure <- makeCounter
print =<< closure 1
print =<< closure 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment