Wrong IO actions order using putStr and getLine

This is because ghci disables buffering, while a program compiled with ghc has line buffering by default. You can see this by running this:

import System.IO
main = print =<< hGetBuffering stdout

In ghci you see NoBuffering while with runghc you get LineBuffering. Since the newline character doesn’t print until after the user input, the prompt doesn’t either.

Fix it by adding hFlush stdout after your prompt (or disable buffering with hSetBuffering stdout NoBuffering, but that’s probably bad).

Leave a Comment