[Haskell-cafe] Bug in "Haskell for C programmers" tutorial?

Glynn Clements glynn at gclements.plus.com
Mon Jan 2 07:48:24 EST 2006


Cale Gibbard wrote:

> > > You shouldn't have to flush output manually. Which implementation are
> > > you using? Try importing System.IO and doing:
> > > hGetBuffering stdout >>= print
> > > and see what gets printed. It should be "NoBuffering".
> >
> > The buffering for stdout should be LineBuffering if stdout is a
> > terminal and BlockBuffering otherwise. The buffering for stderr should
> > always be NoBuffering.
> 
> It's actually not, if you're starting your program from ghci, which is
> what confused me. From GHCi, you get NoBuffering on stdout, and
> LineBuffering on stdin, which is sane for interactive programs. Why
> anyone would want LineBuffering as default on stdout is somewhat
> mysterious to me.

Because most programs output entire lines, and the implementation of
Haskell's putStr etc sucks when using NoBuffering (one write() per
character).

C follows the same rules (stdin/stdout use line buffering for
terminals, block buffering otherwise, stderr is always unbuffered),
even though C's puts(), printf() etc behave a lot better with
unbuffered streams (they pass either whole strings or large chunks to
write() rather than individual characters).

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the Haskell-Cafe mailing list