Skip to content

Instantly share code, notes, and snippets.

@lkuper
Created April 5, 2019 07:27
Show Gist options
  • Save lkuper/1597cd0948dd672dcc1286fbb4dbf636 to your computer and use it in GitHub Desktop.
Save lkuper/1597cd0948dd672dcc1286fbb4dbf636 to your computer and use it in GitHub Desktop.
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