Search code examples
haskellrepeat

How can I repeat my function x times in my code?


I would like to repeat the stepcells a for b times. How can I make this ?

play :: Generation -> Int -> Maybe Generation
play a b 
 | b < 0 = Nothing
 | b == 0 = (Just a) 
 | otherwise = Just (stepCells a)  -- do it b times 

Solution

  • As Noughtmare suggests in their comment, iterate is a good solution to this problem. However you can also do it with manual recursion:

    play :: Generation -> Int -> Maybe Generation
    play a b 
     | b < 0 = Nothing
     | b == 0 = (Just a) 
     | otherwise = play (stepCells a) (b - 1)