An IO Question from a Newbie

Brandon Michael Moore brandon at its.caltech.edu
Sun Sep 14 15:11:15 EDT 2003



On Sun, 14 Sep 2003, Glynn Clements wrote:

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

Interesting. I didn't know this. Maybe we should match this behaviour, or
provide a write-string-and-flush function. It seems like this issue
is causing an undue amound of trouble.

> 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

I should note here that there is a gnu readline binding distributed with
GHC. It's undocumented, but it seems to follow the C API pretty closely,
and you can make a decent interface using only two of the functions.

> 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
> Haskell's:
>
> 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 virgin.net>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>



More information about the Haskell-Cafe mailing list