How to do the same n times in Haskell

Sorry if this is a really stupid question, but I already read one book and most of the other book about Haskell and I don't seem to remember that this was brought up.

How can I do the same n times? If you want to know exactly what I'm doing, I am trying to complete some questions from the Google Code Jam to learn Haskell, and the first line of input gives you the number of test cases. In this case, I need to do the same n times, where n is the number of test cases.

The only way I can do this so far is to write a recursive function as follows:

recFun :: Int -> IO () -> IO ()
recFun 0 f = do return ()
recFun n f = do
    f
    recFun (n-1) f
    return ()

Is there a built-in function that already does this?

+4
source share
1 answer

forM_from Control.Monadis one way.

Example:

import Control.Monad (forM_) 

main = forM_ [1..10] $ \_ -> do
    print "We'll do this 10 times!"

See documentation here

http://hackage.haskell.org/package/base-4.8.0.0/docs/Control-Monad.html#v:forM-95-

+5
source

All Articles