An IO Question from a Newbie

Glynn Clements glynn.clements at
Sun Sep 14 17:09:37 EDT 2003

Brandon Michael Moore wrote:

> 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:

But there's one significant difference between C and Haskell, which is
applicable in the case of Matt's program. In C, any line-buffered
output streams are automatically flushed when a read from an
unbuffered or line-buffered stream can't be satisfied from its buffer.

Also, it seemed fairly clear from Matt's original message that:

a) he didn't want to have to force a new-line (he noted that doing so
eliminated the problem), and

b) he understood the concept of flushing, but presumably didn't know
how to do it in Haskell.

While we're on the subject, I'll point out a couple of other
differences between the buffering in ANSI C's stdio library and

1. In Haskell, you can change the buffering mode at any point; in C,
you have to change it before any data is read from or written to the
stream, otherwise the behaviour is undefined.

2. For an input stream which is associated with a tty, changing the
buffering mode may also change the terminal settings (setting it to
unbuffered disables canonical mode while setting it to line-buffered
or fully-buffered enables it).

Glynn Clements <glynn.clements at>

More information about the Haskell-Cafe mailing list