Skip to main content
added 315 characters in body
Source Link
leftaroundabout
  • 121k
  • 6
  • 185
  • 336
import Data.IORef
import Control.Monad

main = do
   i <- newIORef 0
   
   let loop = do
        iNow <- readIORef i
        when (iNow < 10) $ do
          print i
          modifyIORef i (+1)
          loop
   loop

But obviously, you should avoid this. mapM_ print [0..9] works much better.


I see you have to i variables in there with different increment. Well, it's obvious how to add that here. You can stay to the manual recursion through loop. A little better is replacing one IORef by a simple forM_. Preferrably, try not to use any IORef at all but only functional structures.

import Data.IORef
import Control.Monad

main = do
   i <- newIORef 0
   
   let loop = do
        iNow <- readIORef i
        when (iNow < 10) $ do
          print i
          modifyIORef i (+1)
          loop
   loop

But obviously, you should avoid this. mapM_ print [0..9] works much better.

import Data.IORef
import Control.Monad

main = do
   i <- newIORef 0
   
   let loop = do
        iNow <- readIORef i
        when (iNow < 10) $ do
          print i
          modifyIORef i (+1)
          loop
   loop

But obviously, you should avoid this. mapM_ print [0..9] works much better.


I see you have to i variables in there with different increment. Well, it's obvious how to add that here. You can stay to the manual recursion through loop. A little better is replacing one IORef by a simple forM_. Preferrably, try not to use any IORef at all but only functional structures.

Source Link
leftaroundabout
  • 121k
  • 6
  • 185
  • 336

import Data.IORef
import Control.Monad

main = do
   i <- newIORef 0
   
   let loop = do
        iNow <- readIORef i
        when (iNow < 10) $ do
          print i
          modifyIORef i (+1)
          loop
   loop

But obviously, you should avoid this. mapM_ print [0..9] works much better.