Search code examples
haskellrecursioniodo-notation

Haskell recursion in IO action


Still a beginner that can't figure out a recursive loop in an IO action. Assume:

fMinInspect :: Int
fMinInspect = 1
fMaxInspect :: Int
fMaxInspect = 12
-- fNoInspectPerHour :: IO ()
fNoInspectPerHour = do
  generateInspect <- randomRIO (fMinInspect,fMaxInspect)
  putStrLn ""

I would like to generate a list for let's say 10 machines I want randomly checked, can I then make a repeated call to something that would add 10x fNoInspectPerHour to a list?

I tried an external function, but I can't get the actions from the IO (). Note that putStrLn is just to terminate the do-block, since I don't need console output.

PS. suggestions on terminating a do-block without the putStrLn is welcome too.


Solution

  • If you actually meant to use recursion yourself here, you could write e.g.

    fNoInspectPerHour :: Int -> IO () 
    fNoInspectPerHour 0 = return () 
    fNoInspectPerHour n = do { 
        generateInspect <- randomRIO (fMinInspect,fMaxInspect) ; 
        --  Int            IO Int
        putStrLn generateInspect ;    -- or do some other IO action
        --                 IO ()
        fNoInspectPerHour (n-1) 
        --                 IO ()
        }