'HASKELL -- function to loop over a do block? [closed]

I have a do block and want to loop over it few times.



Solution 1:[1]

There are various ways you can go about doing this.

import Control.Monad

foo :: IO ()
foo = putStrLn "Hello there!"

main :: IO ()
main = do
   let someList = [1,4,9]


   forM_ someList $ \_ -> foo  -- Iterate over all values in the list
                               -- (ignoring the actual value)

   
   mapM_ (const foo) someList  -- Another way to write the same thing

   
   replicateM_ (length someList) foo  -- Repeat a constant number of times


   let go 0 = return ()   -- Small helper function
       go n = do          -- defining the loop
         foo
         go $ n-1
   go $ length someList

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 leftaroundabout