
Why doesn't this work the way it's supposed to, or the way it's intuitively apparent from the code, that is, showing the prompt first, getting the line next, and printing the result finally? main = do putStr "Please Enter Your Name: " name <- getLine putStrLn ("Hello " ++ name) changing putStr with putStrLn rectifies it to the expected behavior, but I wonder why this version misbehaves. FWIW, I use ghc in cygwin. Ali

2010/3/4 Ali Razavi
Why doesn't this work the way it's supposed to, or the way it's intuitively apparent from the code, that is, showing the prompt first, getting the line next, and printing the result finally?
main = do putStr "Please Enter Your Name: " name <- getLine putStrLn ("Hello " ++ name)
changing putStr with putStrLn rectifies it to the expected behavior, but I wonder why this version misbehaves. FWIW, I use ghc in cygwin.
Ali
FWIW using GHCi in standard Windows Vista, this works like it should. -- Deniz Dogan

I am using windows XP, and both in cygwin and cmd, it first pauses to get
the input, and then displays the prompt and the hello message in the same
line.
c:\haskell>runghc Test.hs
test
Please Enter Your Name: Hello test
Ali
On Thu, Mar 4, 2010 at 1:13 PM, Deniz Dogan
Why doesn't this work the way it's supposed to, or the way it's intuitively apparent from the code, that is, showing the prompt first, getting the
2010/3/4 Ali Razavi
: line next, and printing the result finally?
main = do putStr "Please Enter Your Name: " name <- getLine putStrLn ("Hello " ++ name)
changing putStr with putStrLn rectifies it to the expected behavior, but I wonder why this version misbehaves. FWIW, I use ghc in cygwin.
Ali
FWIW using GHCi in standard Windows Vista, this works like it should.
-- Deniz Dogan

On Thu, Mar 04, 2010 at 01:06:42PM -0500, Ali Razavi wrote:
Why doesn't this work the way it's supposed to, or the way it's intuitively apparent from the code, that is, showing the prompt first, getting the line next, and printing the result finally?
main = do putStr "Please Enter Your Name: " name <- getLine putStrLn ("Hello " ++ name)
changing putStr with putStrLn rectifies it to the expected behavior, but I wonder why this version misbehaves. FWIW, I use ghc in cygwin.
Ali
This is because of output buffering. By default, LineBuffering is used, which means that the runtime will collect output in a buffer until seeing a newline, at which point it will actually print the buffer contents on the screen. This is why changing the putStr to putStrLn makes it work. You can turn off output buffering like so: import System.IO main = do hSetBuffering stdout NoBuffering putStr "Please Enter Your Name: " ... etc. -Brent

On Thu, Mar 4, 2010 at 7:30 PM, Brent Yorgey
Why doesn't this work the way it's supposed to, or the way it's intuitively apparent from the code, that is, showing the prompt first, getting the
On Thu, Mar 04, 2010 at 01:06:42PM -0500, Ali Razavi wrote: line
next, and printing the result finally?
main = do putStr "Please Enter Your Name: " name <- getLine putStrLn ("Hello " ++ name)
changing putStr with putStrLn rectifies it to the expected behavior, but I wonder why this version misbehaves. FWIW, I use ghc in cygwin.
Ali
This is because of output buffering. By default, LineBuffering is used, which means that the runtime will collect output in a buffer until seeing a newline, at which point it will actually print the buffer contents on the screen. This is why changing the putStr to putStrLn makes it work. You can turn off output buffering like so:
import System.IO
main = do hSetBuffering stdout NoBuffering putStr "Please Enter Your Name: " ... etc.
If you don't want a newline after the prompt you can use `hFlush stdout` to manually flush the buffer. Cheers, Johan

Am Donnerstag 04 März 2010 19:30:17 schrieb Brent Yorgey:
On Thu, Mar 04, 2010 at 01:06:42PM -0500, Ali Razavi wrote:
Why doesn't this work the way it's supposed to, or the way it's intuitively apparent from the code, that is, showing the prompt first, getting the line next, and printing the result finally?
main = do putStr "Please Enter Your Name: " name <- getLine putStrLn ("Hello " ++ name)
changing putStr with putStrLn rectifies it to the expected behavior, but I wonder why this version misbehaves. FWIW, I use ghc in cygwin.
Ali
This is because of output buffering. By default, LineBuffering is used, which means that the runtime will collect output in a buffer until seeing a newline, at which point it will actually print the buffer contents on the screen. This is why changing the putStr to putStrLn makes it work. You can turn off output buffering like so:
import System.IO
main = do hSetBuffering stdout NoBuffering putStr "Please Enter Your Name: " ... etc.
-Brent
Another option is to explicitly flush stdout: import System.IO main = do putStr "Please enter your name: " hFlush stdout name <- getLine putStrLn $ "Hello " ++ name Whether globally setting the buffering to NoBuffering is better or manually flushing the handle in a couple of places depends of course on the programme flow (lots of output with few prompts in between, flush manually; lots of prompts and little other output, set buffering).

On Thu, 2010-03-04 at 13:30 -0500, Brent Yorgey wrote:
On Thu, Mar 04, 2010 at 01:06:42PM -0500, Ali Razavi wrote:
Why doesn't this work the way it's supposed to, or the way it's intuitively apparent from the code, that is, showing the prompt first, getting the line next, and printing the result finally?
main = do putStr "Please Enter Your Name: " name <- getLine putStrLn ("Hello " ++ name)
changing putStr with putStrLn rectifies it to the expected behavior, but I wonder why this version misbehaves. FWIW, I use ghc in cygwin.
Ali
This is because of output buffering. By default, LineBuffering is used, which means that the runtime will collect output in a buffer until seeing a newline, at which point it will actually print the buffer contents on the screen. This is why changing the putStr to putStrLn makes it work. You can turn off output buffering like so:
import System.IO
main = do hSetBuffering stdout NoBuffering putStr "Please Enter Your Name: " ... etc.
-Brent
It is also possible to use hFlush from System.IO to force a flush on stdout. main = do putStr "Hi, " hFlush stdout name <- getLine putStrLn ("You really do like flowers, miss " ++ name)
participants (6)
-
Ali Razavi
-
Brent Yorgey
-
Daniel Fischer
-
Deniz Dogan
-
Gabríel A. Pétursson
-
Johan Tibell