Haskell: Why does this function keep asking for user input rather than terminating

I am learning some Haskell and I came across this little program

reverseLines :: String -> String
reverseLines input = 
    unlines (map reverse (lines input))

main :: IO ()
main = interact reverseLines

This program will continue to ask the user for more input and cancel the input and print it on the screen.

Most of this is straight forward, but one thing I can’t wrap my head with is why this function continues to work and asks the user for more input, whereas if I just replaced the function reverseLineswith a function, just returning some line it will not happen.

This program will stop after one run:

foo input = "Stops"
main :: IO ()
main = interact foo

Why?

+4
source share
1 answer

interact, :

interact f      =   do s <- getContents
                       putStr (f s)

. getContents? - EOF

Haskell lazy-IO, , - . reverseLines - , , , \n (lines), , REPL.

,

, ( echo) :

echo "Hello World\nBye Bye" | runhaskell LazyIO.hs

CTRL-D EOF .

, - , -, ( reverse )? words lines,...?

!

+4

All Articles