-
-
Save bheklilr/7331ab8d54ba17d82cae to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| {-# 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