Skip to content

Instantly share code, notes, and snippets.

@CindyLinz
Created March 3, 2013 13:40
Show Gist options
  • Save CindyLinz/5076125 to your computer and use it in GitHub Desktop.
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'.
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