"interact" behaves oddly if used interactively

Simon Marlow simonmar at microsoft.com
Tue Sep 30 18:51:57 EDT 2003


 
> For GHC (6.0.1)
> 
> "main=interact id" basically echoes every line of my input, whereas
> "main=interact show" correctly waits for EOF before 
> outputting something.

Which of these are you claiming is wrong?  This is the behaviour I'd
expect in both cases (although I'd have to go and carefully review the
code for 'show :: String -> String' in the report to be able to tell
whether it was supposed to be that strict or not).

> Furthermore the buffering mode must be "LineBuffering".

The buffering mode of a Handle attached to a terminal is LineBuffering
by default.

> If I explicitely set the buffering to "NoBuffering" I'm not able to 
> enter EOF by typing "ctrl-d".

This is a consequence of Unixy terminal semantics.  Unix does line
buffering on terminals in the kernel as part of the terminal processing,
unless you turn it off.  However, the Ctrl-D processing is also part of
the terminal processing which gets turned off when the kernel line
buffering is disabled.  So GHC tries its best to get NoBuffering
semantics by turning off the kernel line buffering, but as a result you
lose Ctrl-D.  C'est la vie.

I've added this to the FAQ.

> Furthermore my terminal seems 
> to remain in 
> the buffering mode set by the previously called ghc-haskell program 
> (because it effects programs that do not "hSetBuffering".)

This is a bug, recently fixed.

> What should a student think about "interact" in the Prelude? (It's ok 
> for pipes only, I guess.)

Well, combining laziness with I/O is always going to be problematic.
I'd say steer clear of introducing students to interact.

Cheers,
	Simon



More information about the Haskell mailing list