I am new to haskell programming. I would like an example code of how I can quit a main program by entering a command (for example QUIT)and go back to the default Prelude> screen. I am using the GHC 7.8.3 interpreter. Please also mention which which module(s) I would need to import if any. I have been searching all over and trying different things but nothing seems to work. Really want to know how to do this asap. Thank you very much in advance
Asked
Active
Viewed 6,938 times
2 Answers
8
You can use one of the functions from the System.Exit
module. The simplest use would probably be something like this:
import System.Exit (exitSuccess)
main = exitSuccess
Of course, it is not of great utility in this example, but you can place it anywhere an IO ()
can be used, and it will terminate the program. In GHCi, the exception it throws will be caught, and you will return to the Prelude>
prompt after an *** Exception: ExitSuccess
line.

icktoofay
- 126,289
- 21
- 250
- 231
2
This SO answer contains a basic read-execute loop: https://stackoverflow.com/a/27094682/866915
When you see the Prelude>
prompt you are operating within the program ghci
, and you will get back to that prompt when the function you've called returns.
An abbreviated example:
main = do
let loop = do putStr "Type QUIT to quit: "
str <- getLine
if str == "QUIT"
then return ()
else loop
loop
-
Very good. On point of what I was looking for. My knowlege is increasing and now I can pratice some of my own variations :). Thank you very much – gin Nov 30 '14 at 00:49