Skip to content

Instantly share code, notes, and snippets.

@lkuper lkuper/Main.hs
Created Apr 5, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.