[Haskell] putStr is not evaluated in the correct order

Jeremy Shaw jeremy.shaw at linspireinc.com
Sat Sep 2 20:11:33 EDT 2006


GHCi and the compiled program do not buffer the output in quite the
same way. In the compile program, stdout is line buffered, so it will
not output anything until it gets a '\n'. You can force the output
using 'hFlush stdout':

> import System.IO
> main = do
>    putStr "Who are you? "
>    hFlush stdout
>    name <- getLine
>    putStrLn ("Hello, " ++ name)

You could also change stdout to be unbuffered -- but using flush is
probably the prefered method:

> main = do
>    hSetBuffering stdout NoBuffering
>    putStr "Who are you? "
>    name <- getLine
>    putStrLn ("Hello, " ++ name)

Either solution should make the GHCi and compiled version behave the

The behaviour of the compiled version is consistent with most other
languages under Unix. The behaviour under GHCi could be considered
buggy -- it has certainly confused many people. On the other hand, I
think the GHCi behaviour is also somewhat deliberate.


More information about the Haskell mailing list