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.
-
putStrLn ("Enter your name: ")- Displays the prompt message to enter the name.
-
name <- getLine- Reads user input as a string and stores it in
name.
- Reads user input as a string and stores it in
-
putStrLn ("Welcome " ++ name)- Concatenates
"Welcome "with the input stringnameusing++and prints the result.
- Concatenates
Key Takeaways
getLinereads a string input from the user.++is used for string concatenation in Haskell.putStrLnprints 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