gin
gin

Reputation: 68

What is an example implementation of Exit code for Haskell program that returns back to default Prelude> screen GHC 7.8.3

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

Upvotes: 5

Views: 7302

Answers (2)

icktoofay
icktoofay

Reputation: 129001

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.

Upvotes: 8

ErikR
ErikR

Reputation: 52029

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

Upvotes: 2

Related Questions