Haskell newIORef readIORef writeIORef
Mutable variables are not used often in Haskell, but there is time that you need to
use mutable variables in your code. There are many ways to create mutable variables.
Here is how to use mutable variables in Haskell
The idea is very simple:
1. Create a mutable variable
2. Modify or write the variable
3. Read the variable whatever the new value is inside.
var <- newIORef 2
x <- readIORef var
print x
writeIORef var 100
x1 <- readIORef var
print x1
-------------------------------------------------------------------
import Data.IORef
ref <- newIORef 0
replicateM_ 4 $ modifyIORef ref (+5)
readIORef ref >>= print -- output 20
ref <- newIORef 1
replicateM_ 4 $ modifyIORef ref (+5)
readIORef ref >>= print -- output 21
-- modify (Set Int)
set <- newIORef Set.empty
modifyIORef set (Set.insert 2)
set <- readIORef set
Haskell Mutable Loop, it is wrong way to do thing
total <- newIORef 0
let loop i =
if i > 100
then do
readIORef total
else do
modifyIORef total (+1)
pp $ "i=" <<< i
loop (i + 1)
loop 1