Search code examples
haskellexitterminateexit-code

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


Solution

  • 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