Created
March 3, 2013 14:44
-
-
Save CindyLinz/5076364 to your computer and use it in GitHub Desktop.
Another approach, still lazy. But without stack blowup.
This file contains 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 BangPatterns #-} | |
module T where | |
import Control.Concurrent.MVar | |
main = do | |
countMVar <- newMVar (\ count -> count):: IO (MVar (Int -> Int)) | |
let | |
process :: IO () | |
process = do | |
cmd <- getLine | |
modifyMVar_ countMVar $ \ countEater -> return $ \ count -> countEater $! count + 1 | |
if cmd == "out" | |
then modifyMVar_ countMVar $ \ countEater -> do | |
let | |
now = countEater 0 | |
putStrLn $ show now | |
return $ \ count -> now + count | |
else return () | |
process | |
process |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment