Skip to content

Instantly share code, notes, and snippets.

@NJichev
Created October 18, 2018 14:00
Show Gist options
  • Save NJichev/9d7084daf1992ce8855c6df5f8fc5c33 to your computer and use it in GitHub Desktop.
Save NJichev/9d7084daf1992ce8855c6df5f8fc5c33 to your computer and use it in GitHub Desktop.
import Data.IORef
main :: IO ()
main = do
putStrLn "I'm going to calculate a sum, hang on a sec"
totalRef <- newIORef (0 :: Int)
let loop i
| i > 100 = pure ()
| otherwise = do
oldTotal <- readIORef totalRef
let newTotal = oldTotal + i
writeIORef totalRef $! newTotal
loop $! i + 1
loop 1
total <- readIORef totalRef
putStrLn $ "The total is " ++ show total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment