[Haskell-beginners] [IO String] to IO [String]
Ovidiu D
ovidiudeac at gmail.com
Mon Apr 1 11:54:57 CEST 2013
On Mon, Apr 1, 2013 at 11:52 AM, Lyndon Maydwell <maydwell at gmail.com> 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 [] = []
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130401/8c10f6d4/attachment.htm>
More information about the Beginners
mailing list