-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleREPLstate.hs
More file actions
30 lines (25 loc) · 868 Bytes
/
Copy pathsimpleREPLstate.hs
File metadata and controls
30 lines (25 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{-# LANGUAGE FlexibleContexts #-}
import System.IO (hSetBuffering, stdin, stdout, BufferMode(..))
import Control.Monad.State
parseInput :: String -> Int
parseInput = read
insertScore :: (MonadState Int m) => Int -> m ()
insertScore input = modify (+ input)
calcScore :: (MonadState Int m) => m Int
calcScore = get
score :: (MonadState Int m, MonadIO m) => m ()
score = do
val <- liftIO $ putStr "> " >> getLine
let input = parseInput val
if input == 0 then do
score <- calcScore
liftIO $ print ("exiting with score: " ++ show score)
else do
liftIO $ print ("Given Input is: " ++ show input)
insertScore input
score
main = do
-- this Buffering is for https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=846279
hSetBuffering stdout NoBuffering
hSetBuffering stdin NoBuffering
evalStateT score 0