Skip to content

Instantly share code, notes, and snippets.

@bergmark
Last active March 16, 2021 12:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Yes, haskell has for loops
module Main where
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Trans.State
main :: IO ()
main = do
forLoop 0 (< 10) (+ 1)
print
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