Created
April 5, 2019 07:27
-
-
Save lkuper/1597cd0948dd672dcc1286fbb4dbf636 to your computer and use it in GitHub Desktop.
This file contains 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
module Main where | |
import Data.IORef | |
data Counter = Counter { x :: IORef Int } | |
makeCounter :: Int -> IO Counter | |
makeCounter i = do iref <- newIORef i | |
return (Counter iref) | |
incCounter :: Int -> Counter -> IO () | |
incCounter i (Counter c) = do modifyIORef c (+ i) | |
showCounter :: Counter -> IO () | |
showCounter (Counter c) = do c' <- readIORef c | |
print(c') | |
main :: IO () | |
main = do | |
c <- makeCounter 1 | |
showCounter c | |
incCounter 1 c | |
showCounter c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment