Yes, haskell has for loops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Control.Monad | |
import Control.Monad.IO.Class | |
import Control.Monad.Trans.State | |
main :: IO () | |
main = do | |
forLoop 0 (< 10) (+ 1) | |
forLoop :: i -> (i -> Bool) -> (i -> i) -> (i -> IO ()) -> IO () | |
forLoop init pred change run = | |
void $ flip runStateT init $ iter pred change run | |
iter :: (i -> Bool) -> (i -> i) -> (i -> IO ()) -> StateT i IO () | |
iter pred change run = do | |
i <- get | |
if pred i | |
then do | |
liftIO (run i) | |
modify change | |
iter pred change run | |
else pure () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment