Created
March 3, 2013 13:40
-
-
Save CindyLinz/5076125 to your computer and use it in GitHub Desktop.
This program will steadily read lines from STDIN. When meet a line with "out", it will print out the line number.
BUT. if there are many many many many lines before the "out", then the "out" will cause the program crash because of the lazy evaluation 'count + 1'. My solution is add a 'seq' to force the evaluation 'count + 1'.
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
module T where | |
import Control.Concurrent.MVar | |
main = do | |
countMVar <- newMVar 0 :: IO (MVar Int) | |
let | |
process :: IO () | |
process = do | |
cmd <- getLine | |
modifyMVar_ countMVar $ \ count -> return $ count + 1 | |
if cmd == "out" | |
then withMVar countMVar $ \ count -> putStrLn $ show count | |
else return () | |
process | |
process |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment