You need the mapM_ combinator, which displays a function that returns a monadic value in a list and uses the binding operator to sequence the results:
>> let foo n = putStrLn (show n ++ "!") >> mapM_ foo [0,1,0,2,3] 0! 1! 0! 2! 3!
Sometimes people like to use the inverted version.
for :: Monad m => [a] -> (a -> mb) -> m () for = flip mapM_
which is more like an imperative code:
>> for [1..5] $ \n -> putStrLn ("Number: " ++ show n) Number: 1 Number: 2 Number: 3 Number: 4 Number: 5
Note that the combinator named forM_ is defined in Control.Monad and does the same thing as the combinator that I called for .
Chris taylor
source share