
Ah I see what you're saying.
interact [1] uses hGetContents [2] to read the user-input. This is a
lazy-io function which is also used in readFile [3]. In the same way that
readFile won't read the entire file if its contents aren't used, neither
will interact. There is debate as to weather this is a good way to handle
(lol) IO, with other solutions including Conduit [4] and Pipes [5], but for
simple programs it is an elegant solution.
[1]
http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/System-...
[2]
http://hackage.haskell.org/packages/archive/base/latest/doc/html/System-IO.h...
[3]
http://hackage.haskell.org/packages/archive/base/4.3.1.0/doc/html/src/System...
[4] http://hackage.haskell.org/package/conduit
[5] http://hackage.haskell.org/package/pipes
I hope this helps :)
On Mon, Apr 1, 2013 at 5:54 PM, Ovidiu D
On Mon, Apr 1, 2013 at 11:52 AM, Lyndon Maydwell
wrote: The untilexit definition is pretty straightforward. You can see that the the function operates on a string. There are two cases where nothing is returned: "" and "exit" ++ rest.
Ok, that's clear. What I don't understand is why does it stop interact? It returns the same thing if the input is empty or if it starts with "exit". Why doesn't it stop interact when the input line is empty?
In order to display a prompt before each input you will need to actually display a prompt /after/ each line, and an additional prompt before you start processing input.
I was trying to do that but I was doing it wrong. Anyway this is the result which works as expected now, although I don't really understand why it stops at 'exit'
import System.IO
prompt = ">> "
main :: IO () main = do hSetBuffering stdout NoBuffering putStr prompt interact $ display . map reverse . lines . untilexit
display = concat . map appendPrompt where appendPrompt line = line ++ "\n" ++ prompt
untilexit :: String -> String untilexit ('e':'x':'i':'t':_) = [] untilexit (c:t) = c : untilexit t untilexit [] = []
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners