如何在我的代码中重复我的函数 x 次?
How can I repeat my function x times in my code?
我想重复步骤单元a b 次。我该怎么做?
play :: Generation -> Int -> Maybe Generation
play a b
| b < 0 = Nothing
| b == 0 = (Just a)
| otherwise = Just (stepCells a) -- do it b times
正如 Noughtmare 在 中建议的那样,iterate
是解决此问题的好方法。但是,您也可以通过手动递归来完成:
play :: Generation -> Int -> Maybe Generation
play a b
| b < 0 = Nothing
| b == 0 = (Just a)
| otherwise = play (stepCells a) (b - 1)
我想重复步骤单元a b 次。我该怎么做?
play :: Generation -> Int -> Maybe Generation
play a b
| b < 0 = Nothing
| b == 0 = (Just a)
| otherwise = Just (stepCells a) -- do it b times
正如 Noughtmare 在 iterate
是解决此问题的好方法。但是,您也可以通过手动递归来完成:
play :: Generation -> Int -> Maybe Generation
play a b
| b < 0 = Nothing
| b == 0 = (Just a)
| otherwise = play (stepCells a) (b - 1)