Skip to content

Instantly share code, notes, and snippets.

@AndrasKovacs
Created October 12, 2013 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrasKovacs/6948657 to your computer and use it in GitHub Desktop.
Save AndrasKovacs/6948657 to your computer and use it in GitHub Desktop.
Backwards goto and looping via the continuation monad.
import Control.Monad.Cont
import Data.Function
getLabel = ContT fix
goto label = lift label
getLoop x = ContT (\k -> let f x' = ContT (\_ -> k (f, x')) in k (f, x))
gotoTest :: IO ()
gotoTest = (`runContT` return) $ do
loop <- getLabel
word <- lift getLine
case word of
"exit" -> return ()
_ -> do
lift $ putStrLn $ "you typed: " ++ word
goto loop
lift $ putStrLn "bye"
loopTest :: IO ()
loopTest = (`runContT` return) $ do
(loop, i) <- getLoop 10
word <- lift getLine
if i == 0
then return ()
else do
lift $ putStrLn $ "loops remaining: " ++ show i
loop (i - 1)
lift $ putStrLn "bye"
main = loopTest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment