Created
May 1, 2017 11:50
-
-
Save jstimpfle/cb375a7c7d2fb0e840491cc6fe7c8407 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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