Created
October 18, 2018 14:00
-
-
Save NJichev/9d7084daf1992ce8855c6df5f8fc5c33 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
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