
Hal was pretty terse, so I'll explain why switching to putStrLn will help.
stdout is line buffered.
At least by default (see hSetBuffering). That means output will only be
flushed to the screen once a newline is written. Your prompt wasn't
printed because it didn't have a newline, so it was buffered until the
second print provided one (read from the user, by way of s).
This is hardly specific to Haskell. Try this C program:
#import
This is a buffering problem. Use hSetBuffering to fix this (see Chapter 3 in YAHT -- www.isi.edu/~hdaume/htut/). Alternatively, use:
main = do putStrLn "Type Something:" ...
in which case the "Ln" part will force it to be printed.
- Hal
On Fri, 12 Sep 2003, Matt O'Connor wrote:
Hello all. I'm new to functional programming and Haskell, but have been programming in C and Java for a while. I've been going through the tutorials and whatnot on haskell.org. I've read from the Gentle Introduction to Haskell about IO and some of the other stuff and I have a question about it.
main = do putStr "Type Something: " str <- getLine putStrLn ("You typed: " ++ str)
When compile and run this code the "Type Something: " isn't displayed until the putStrLn. So there is no prompt. The output looks like this.
s Type Something: You typed: s
But if I change the putStr "Type Something: " to a putStrLn or put a \n at the end of the string it displays the text immediately (ie, when I want it to). Is there a good reason for this? Am I doing something wrong? Or do I need to call some kind of standard output flush? Thanks.
Oh, I'm using ghc.
Matt
-- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe