0

I'm using GHC 8.6.5 in Windows 10.

I'm a beginner in Haskell, but have some C# experience. When I try main = putStrLn "Hello, World!" and I compile, the program shows the screen only for a moment. I want something like Console.ReadLine() in C# so that the screen will remain until I press enter. What should I do?

I have searched the net for 2 hours in vain.

main = putStrLn "Hello, World!"
3
  • 1
    While learning Haskell, you can also use GHCi, which is a REPL, and load your .hs file, avoiding main. In GHCi you can evaluate any expression and observe their result. If you edit the .hs file, you can use :r to reload the new definitions. Commented May 28, 2019 at 16:49
  • Next time, use hoogle to search for library functions. Commented May 28, 2019 at 16:56
  • 1
    There is no Haskell 8.6.5. If you mean ghc 8.6.5, say so. Commented May 28, 2019 at 17:18

1 Answer 1

3

getLine reads a whole line (returning that as a string, but you don't need that).

main = do
   putStrLn "Hello, World!"
   getLine

In this way, you'll need to press enter to exit the program.

Sign up to request clarification or add additional context in comments.

1 Comment

Side note: this relies on a small quirk of main, that its type is allowed to be IO x for any type x, and the result is simply ignored. To discard the result of getLine explicitly, you can write main :: IO (); do { putStrLn "..."; _ <- getLine; pure () } or void getLine using void from Control.Monad.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.