An IO Question from a Newbie

Glynn Clements glynn.clements at virgin.net
Sat Sep 13 01:42:41 EDT 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.

Yes; if you don't want a newline after the prompt, you need to use:

	hFlush stdout

to flush the stream.

-- 
Glynn Clements <glynn.clements at virgin.net>


More information about the Haskell-Cafe mailing list