Let's say we have a short haskell programm:
main = do putStr "2 + 2 = "
x <- readLn
if x == 4
then putStrLn "Correct"
else putStrLn "Wrong"
What output does it produce?
4
2 + 2 = Correct
Now let's have another:
main = do putStrLn "2 + 2 = "
x <- readLn
if x == 4
then putStrLn "Correct"
else putStrLn "Wrong"
That produces
2 + 2 =
4
Correct
Where the bold 4 is user-inputted.
Could anybody familiar with Haskell explain to me why that is? And how do I get the desired result, which is
2 + 2 = 4
Correct
Line buffering. The output buffer is not "flushed" until a complete line of text is written.
Two solutions:
putStr
followed by hFlush stdout
.)hSetBuffering stdout NoBuffering
.)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With