"interact" behaves oddly if used interactively

Keith Wansbrough Keith.Wansbrough at cl.cam.ac.uk
Wed Oct 1 17:02:01 EDT 2003


> Allow me to have another opinion, if the consequence is interleaved in- 
> and output (when I don't want it).
> 
> Can actually someone supply an implementation of something like interact 
> that does no pipelining for the argument "id"? Simply doing "putStr !$ f 
> !$ s" was not enough!

Yes, of course.

Your code above only forces the evaluation of the first cons-cell of
the list, which is not enough.  You want to force the entire list.
Try

deepSeq :: [a] -> b -> b
deepSeq (x:xs) y = deepSeq xs y
deepSeq [] y = y

noninteract f = do
  s <- getContents
  putStr (f (deepSeq s s))

or if you want non-lazy output too,

reallynoninteract f = do
  s <- get Contents
  let r = f (deepSeq s s)
  putStr (deepSeq r r)

<warn>untested code!</warn>

There's a library containing such functions, called (IIRC) DeepSeq or
something similar.

HTH.

--KW 8-)



More information about the Haskell mailing list