I am trying to produce an interactive Haskell program using the interact
function with map
.
Here's what I get in ghci (as far as I can tell, that's the way all tutorials explain interact
usage -- except the result).
*Module> interact $ unlines . map (++ "!") . lines
tteesstt
!
Note that what actually happens is that every character I type is instantly repeated and after I press Return the exclamation mark appears. I was, however, expecting this:
*Module> interact $ unlines . map (++ "!") . lines
test
test!
It works perfectly if I use the same program structure, but filter
instead of map
.
The problem is that ghci changes the buffering mode to per-character. This is, that the program starts to process the code as soon as it is there. If you write this line into a file called foo.hs
main = interact $ unlines . map (++ "!") . lines
and run it using runhaskell foo.hs
you will see that it works as expected, because Haskell uses line-buffering by default.