Haskell Program to print string entered by the user

Program

main = do
 putStrLn ("Enter your name: ")
 name <- getLine
 putStrLn ("Welcome " ++ name)

This Haskell program takes a string input (user's name) and prints a welcome message.

  1. putStrLn ("Enter your name: ")

    • Displays the prompt message to enter the name.
  2. name <- getLine

    • Reads user input as a string and stores it in name.
  3. putStrLn ("Welcome " ++ name)

    • Concatenates "Welcome " with the input string name using ++ and prints the result.

Key Takeaways

  • getLine reads a string input from the user.
  • ++ is used for string concatenation in Haskell.
  • putStrLn prints a string followed by a newline.

Output

$ ghc print_string.hs
[1 of 1] Compiling Main             ( print_string.hs, print_string.o )
Linking print_string ...
$ ./print_string 
Enter your name: 
User
Welcome User