"interact" behaves oddly if used interactively

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Wed Oct 1 13:29:28 EDT 2003

Christian Maeder <maeder at tzi.de> writes:

> I'm not happy that interleaving depends on the strictness. Lazy or 
> strict evaluation should only change the behaviour of overall 
> termination (lazy evaluation should terminate more often).

But the whole purpose of 'interact' is to use its argument as the
demanding function which drives lazy consumption of the input.  It is
*designed* to reveal the evaluation behaviour, by hoisting it into
the I/O monad.

(AFAIK, 'interact' was explicitly designed for beginners, as an easy
way to turn a pure computation into one which will actually run,
before they learn about the I/O monad.  Anyone from the original
Haskell committee should feel free to correct me at this point,
but interact was never intended for "serious" users.)

> I'ld rather 
> implement interleaving (or "interactive" behaviour) explicitely by:
> interact f = do
>    s <- getLine
>    putStrLn (f s)
>    interact f

Your suggested implementation does not have anything like the same
semantics as the current 'interact'.  For instance, consider the
following program to number the lines of its input:

    main = interact (unlines . zipWith lineno [0..] . lines)
      where lineno n s = shows n (" "++s)

Your version of interact behaves very unintuitively here!

More information about the Haskell mailing list