Excuse me if this is a really dumb question, but I've read through one book and most of another book on Haskell already and don't seem to remember anywhere this was brought up.
How do I do the same thing n times? If you want to know exactly what I am doing, I'm trying to do some of the Google Code Jam questions to learn Haskell, and the first line of the input gives you the number of test cases. That being the case, I need to do the same thing n times where n is the number of test cases.
The only way I can think of to do this so far is to write a recursive function like this:
recFun :: Int -> IO () -> IO ()
recFun 0 f = do return ()
recFun n f = do
f
recFun (n-1) f
return ()
Is there no built in function that already does this?
forM_
from Control.Monad
is one way.
Example:
import Control.Monad (forM_)
main = forM_ [1..10] $ \_ -> do
print "We'll do this 10 times!"
See the documentation here
http://hackage.haskell.org/package/base-4.8.0.0/docs/Control-Monad.html#v:forM-95-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With