[Haskell-cafe] Tracking characters and a timestamp ?

Marc Weber marco-oweber at gmx.de
Wed Apr 4 13:34:05 EDT 2007

=============  =======================================================
 This program should read characters from stdin, stop on 's' and print the character along with a timestamp
 You need ghc-6.6 because of Date.Time.
 When not using mapM addTimeCode I'll get the lines printed on stdout instantly

import Data.Time.Clock
import Data.Time
import Control.Monad

handleChar :: Show a => (Char, a) -> IO ()
handleChar ('s', _) = exitWith (ExitFailure 1)
handleChar tuple = print tuple

addTimeCode a = liftM ( (,) a) getCurrentTime

main = do
  hSetBuffering stdin NoBuffering
  hGetContents stdin >>= mapM addTimeCode >>= mapM_ handleChar

=============  ======================================================

When running and typing a b c it should print
('a', <timestamp>)
('b', <timestamp>)
('c', <timestamp>)

It seems to wait till the end of the infinite list. Why?

When using a take 10  I'll get those 10 events after having typed the
10th character.

Do I need continuation passing style?
Would unsafeInterleaveIO help?

The final goal is writing a timetracker which sums up the time you've
spent on different wmii tags (= you can think of them beeing different screens )

Marc Weber

More information about the Haskell-Cafe mailing list