[Haskell-beginners] IO question

Brandon Allbery allbery.b at gmail.com
Fri Oct 28 18:17:53 CEST 2011


On Fri, Oct 28, 2011 at 11:01, Ertugrul Soeylemez <es at ertes.de> wrote:

> Brandon Allbery <allbery.b at gmail.com> wrote:
> > The code is doing exactly what it says.  getTwoChars reads two
> > characters --- *not* a line.  So the next character waiting to be read
> > is the newline, which is returned by the next getTwoChars.
> >
> > This may depend to some extent on the platform, as Unix defaults to a
> > line-oriented input interface (at the OS level) but Windows to
> > character-oriented input.
>
> I can't confirm that.  This is true for compiled programs, but when
> running
>
>    liftA2 (,) getChar getChar
>
> on the GHCi command line, it seems to use NoBuffering.


The dependency is a little more complex than simply the tty line buffering
mode; that simply obscures the problem a bit, it doesn't actually modify it.
 Specifically:  in character mode each read terminates without the need to
read a newline, whereas in line mode the newline is needed to pass
characters from the OS to the program; but in either mode the newline is
still read as a separate character.  So in line mode it can *appear* to be
treating each of those as a separate line, when in reality it isn't (and you
find that out when you get the '\n').

(I don't recall offhand if the GHC runtime conflates the file buffering mode
with the tty line mode on Unix.  This is not the file handle buffering mode;
it's the termios "icanon" setting.  But ghci does seem to run with icanon
cleared, at least interactively as distinct from ":main".)

-- 
brandon s allbery                                      allbery.b at gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20111028/fdd6c7a0/attachment-0001.htm>


More information about the Beginners mailing list