[Haskell] How do I handle user input in Haskell?

In Haskell, handling user input typically involves using the getLine function from the System.IO module. Here's a simple example of how to handle user input in Haskell:

1import System.IO
3main :: IO ()
4main = do
5    putStrLn "Enter your name:"
6    name <- getLine
7    putStrLn ("Hello, " ++ name ++ "!")

In this example, the program prompts the user to enter their name, and then reads the input using getLine. The input is stored in the name variable, and finally, the program prints a greeting using the entered name.

You can compile and run this Haskell program using the GHC compiler:

1$ ghc --make main.hs
2$ ./main

When you run the program, it will display the prompt, wait for the user to enter their name, and then print the greeting.

Note that getLine reads a whole line of input until a newline character is encountered. If you need to parse the input into a different data type, you can use functions like read or libraries like Text.Read to convert the string input into the desired type.

Additionally, you can use other functions from System.IO like hGetLine or hGetContents to read input from a file or a different input stream if needed.