3 ms·
Btw, Haskell also supports mutable re-assignment of variables. But it's not something that's built into the language, you get mutable variables via a library.
by eru 2y ago
Btw, Haskell also supports mutable re-assignment of variables. But it's not something that's built into the language, you get mutable variables via a library. Just like you can get loops in Haskell via a library.
- kqr 2y agoOh, yeah, it's right there in the standard library. But one has to be a little more explicit about the fact that one is accessing truly mutable variables. For example, given the helper utilities checkIORef r p = fmap p (readIORef r) usingIORef r a = readIORef r >>= a the example can be written as main = do i <- newIORef (0 :: Int) whileM_ (checkIORef i (< 5)) $ do modifyIORef i (+1) usingIORef i (printf "i: %d\n") That said, even if one actually needs to mix mutable variables with I/O actions like printing, I'm not sure I would recommend using IORefs for it. But opening the can of MonadIO m => StateT Int m () is for another day.