I am using interact
to process some user inputs step-by-step (specifically, it's a chess program). However, I haven't found a way to deal with the situation where the user might want to just break out of the loop and start this match of chess from the beginning.
When I'm executing a normal procedure in ghci, pressing Ctrl-C
will not exit the whole ghci, but will just stop the procedure itself and allow me to go on with some other procedures. However, if I press Ctrl-C
in the console with the interact
function in effect, the following message is shown:
^CInterrupted.
*Main>
<stdin>: hGetChar: illegal operation (handle is closed)
And then I have to launch ghci all over again.
I also thought of catching special user inputs such as "exit", however, since the type of interact
is interact :: (String -> String) -> IO ()
, the input will have to go through the function typed (String -> String)
first, and I haven't found a way for that function to notify the main IO that it should quit.
How should I break out of interact
? Or is interact
not intended to be used this way and I should compose custom IO functions?