different getChar behaviour in interpeter and compiled program

Hi, consider this program: main = do c <- getChar putStrLn ("Entered: " ++ [c]) main (It reads a character and prints it out, for ever). When invoked from interpreter: motiejus@precise> ghci GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Prelude> :load getchar.hs [1 of 1] Compiling Main ( getchar.hs, interpreted ) Ok, modules loaded: Main. *Main> main lEntered: l aEntered: a The behaviour is fine: right after entering a character, I get echo back. However, when started with runhaskell (or compiled ./getchar): motiejus@precise> runhaskell getchar.hs la<CR> Entered: l Entered: a Entered: Behaviour is different: I entered "l", but did not get the "entered" back. Only after I entered carriage-return, I got my multiple echos (including the CR). Why is it so? Is it possible to make Haskell read character-by-character in a compiled program? Clarification: behaviour of $ runhaskell ./getchar.hs is the same as $ ghc ./getchar.hs && ./getchar Ubuntu 12.04.1 i386, Haskell from standard repositories 7.4.1-1ubuntu2. Regards, Motiejus

On Sun, Oct 14, 2012 at 11:14 AM, Motiejus Jakštys
The behaviour is fine: right after entering a character, I get echo back.
However, when started with runhaskell (or compiled ./getchar):
motiejus@precise> runhaskell getchar.hs la<CR> Entered: l Entered: a Entered:
http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci-faq.htmlscroll to the bottom. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix/linux, openafs, kerberos, infrastructure http://sinenomine.net

On Sun, Oct 14, 2012 at 11:31:58AM -0400, Brandon Allbery wrote:
On Sun, Oct 14, 2012 at 11:14 AM, Motiejus Jakštys
wrote: The behaviour is fine: right after entering a character, I get echo back.
However, when started with runhaskell (or compiled ./getchar):
motiejus@precise> runhaskell getchar.hs la<CR> Entered: l Entered: a Entered:
http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci-faq.htmlscroll to the bottom.
Thanks. Not exactly like in the FAQ, but it lead me to the right direction. In my case stdin in ghci is not buffered, whereas in ghc it is line buffered. Turning stdin buffering off before getChar (hSetBuffering stdin NoBuffering) made it work. Regards, Motiejus
participants (2)
-
Brandon Allbery
-
Motiejus Jakštys