[Haskell-beginners] different getChar behaviour in interpeter and compiled program

Motiejus Jakštys desired.mta at gmail.com
Sun Oct 14 17:14:31 CEST 2012


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 at 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 at 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



More information about the Beginners mailing list