Skip to content

Instantly share code, notes, and snippets.

@bheklilr
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save bheklilr/7331ab8d54ba17d82cae to your computer and use it in GitHub Desktop.

Select an option

Save bheklilr/7331ab8d54ba17d82cae to your computer and use it in GitHub Desktop.
{-# LANGUAGE FlexibleContexts #-}
module Main where
import System.IO
import Control.Monad
import Control.Monad.State
type MyApp = StateT [Int] IO
addEntry :: MonadState [Int] m => Int -> m Int
addEntry entry = do
entries <- get
put (entry : entries)
return entry
mainLoop :: [Int] -> IO ()
mainLoop entries = do
entry <- readLn
newEntries <- execStateT (addEntry entry) entries
print newEntries
mainLoop newEntries
mainLoop2 :: MyApp ()
mainLoop2 = do
entry <- liftIO $ do
putStr "Enter a number: "
readLn
modify (entry:)
current <- get
unless (length current > 10) $
liftIO (print current) >> mainLoop2
main :: IO ()
main = do
hSetBuffering stdout NoBuffering
execStateT mainLoop2 [] >>= print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment