Needing strictness?

Busy night. I wanted the question and response to be on the same line (see below), so I used putStr instead of putStrLn, but Haskell jumps the gun and goes to the second line to get the response before the question ever gets printed. Is this an example of needing strictness? How would I do that, or something else, to get the result I want? putStrLn "Some yes/no question (y/n)?" ans <- getLine Michael

Hi Michael, I believe stdout uses line buffering by default so you need to explicitly flush the buffer to get the output -- Johan

Hey Johan,
And I can do that how?
Michael
--- On Wed, 10/21/09, Johan Tibell

Am Donnerstag 22 Oktober 2009 04:15:56 schrieb michael rice:
Busy night. I wanted the question and response to be on the same line (see below), so I used putStr instead of putStrLn, but Haskell jumps the gun and goes to the second line to get the response before the question ever gets printed. Is this an example of needing strictness? How would I do that, or something else, to get the result I want?
putStrLn "Some yes/no question (y/n)?" ans <- getLine
Michael
It's a buffering issue. For compiled code, stdout is by default set to line buffering (it's different for interpreted code, at least on linux). import System.IO do hSetBuffering stdout NoBuffering putStr "Question" ans <- getLine should do what you want.

Am Donnerstag 22 Oktober 2009 04:15:56 schrieb michael rice:
Busy night. I wanted the question and response to be on the same line (see below), so I used putStr instead of putStrLn, but Haskell jumps the gun and goes to the second line to get the response before the question ever gets printed. Is this an example of needing strictness? How would I do that, or something else, to get the result I want?
putStrLn "Some yes/no question (y/n)?" ans <- getLine
Michael
Hit send too quickly: alternatively hFlush stdout between question and answer.

Hi Daniel,
The first fix wouldn't work, but the second one (hFlush) did.
Thanks,
Michael
--- On Wed, 10/21/09, Daniel Fischer
Busy night. I wanted the question and response to be on the same line (see below), so I used putStr instead of putStrLn, but Haskell jumps the gun and goes to the second line to get the response before the question ever gets printed. Is this an example of needing strictness? How would I do that, or something else, to get the result I want?
putStrLn "Some yes/no question (y/n)?" ans <- getLine
Michael
Hit send too quickly: alternatively hFlush stdout between question and answer.
participants (3)
-
Daniel Fischer
-
Johan Tibell
-
michael rice