By Alvin Alexander. Last updated: June 4, 2016
I just ran into a problem where the putStr
function in Haskell was not printed out in the order I wanted it to be. I wanted to prompt a user for input, then read their input, but the putStr
output didn’t appear until later, after I hit the Enter key. (putStr
is preferred here over putStrLn
because it does not add a newline character after the output.)
The solution to this problem is shown in the image, which comes from here. When using putStr you need to flush the output in one of the ways shown.
The following code shows how this works:
main = do putStr "Whaddya want? " hFlush stdout userInput <- getLine