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 stringname
using++
and prints the result.
- Concatenates
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