
I am VERY new to Haskell, and just getting my feet wet with functional programming in general. I've been going over a few examples online, but I can't figure out the behavior I'm seeing on a very basic example: --- module Main where import System.IO main :: IO () main = do putStrLn "Please enter your name: " name <- getLine putStrLn ("Hello, " ++ name ++ ", how are you?") --- This example works as I would expect. The prompt displays with a new line, the input is entered and echoed: --- Please enter your name: joe Hello, joe, how are you? However, changing 'putStrLn' to 'putStr' does not do what I would expect. The prompt doesn't get displayed until after there is input: --- joe Please enter your name: Hello, joe, how are you? Why does it exhibit this behavior? I'm using a ghc 6.6 built from source from 20070227. The regular 6.6 release had the same behavior. Any help would be greatly appreciated. -Joe -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

olivasj:
I am VERY new to Haskell, and just getting my feet wet with functional programming in general. I've been going over a few examples online, but I can't figure out the behavior I'm seeing on a very basic example:
--- module Main where
import System.IO
main :: IO () main = do putStrLn "Please enter your name: " name <- getLine putStrLn ("Hello, " ++ name ++ ", how are you?") ---
This example works as I would expect. The prompt displays with a new line, the input is entered and echoed: --- Please enter your name: joe Hello, joe, how are you?
However, changing 'putStrLn' to 'putStr' does not do what I would expect. The prompt doesn't get displayed until after there is input: --- joe Please enter your name: Hello, joe, how are you?
Why does it exhibit this behavior? I'm using a ghc 6.6 built from source from 20070227. The regular 6.6 release had the same behavior. Any help would be greatly appreciated.
In short: buffering. You're not flushing the output buffer till \n appears. To ask for no buffering, you would write: import System.IO main :: IO () main = do hSetBuffering stdout NoBuffering putStr "Please enter your name: " name <- getLine putStrLn ("Hello, " ++ name ++ ", how are you?") Which runs as: $ ./a.out Please enter your name: Don Hello, Don, how are you? Cheers, Don

Joe Olivas wrote:
However, changing 'putStrLn' to 'putStr' does not do what I would expect. The prompt doesn't get displayed until after there is input:
This isn't a Haskell issue per se. The runtime is putting stdout into line-buffered mode, so you need to import System.IO and use hFlush stdout to get it to flush your prompt string out of its buffer. You'd see the same behaviour in C (if using stdio), Python, etc.

Well, C stdio has this nice thing that if you read from stdin then stdout is automagically flushed first (maybe only if both are ttys). I think Haskell should have that too. -- Lennart On Mar 3, 2007, at 07:10 , Bryan O'Sullivan wrote:
Joe Olivas wrote:
However, changing 'putStrLn' to 'putStr' does not do what I would expect. The prompt doesn't get displayed until after there is input:
This isn't a Haskell issue per se. The runtime is putting stdout into line-buffered mode, so you need to import System.IO and use hFlush stdout to get it to flush your prompt string out of its buffer. You'd see the same behaviour in C (if using stdio), Python, etc.
http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
Bryan O'Sullivan
-
dons@cse.unsw.edu.au
-
Joe Olivas
-
Lennart Augustsson