Haskell Program to convert string to upper case

Program

import Data.Char
main = do
 putStrLn "Enter your name:"
 name <- getLine
 putStrLn "Converted to upper case: "
 putStrLn $ map toUpper name

This Haskell program converts a user-input string to uppercase and prints the result.

  1. import Data.Char

    • Imports the Data.Char module, which provides functions for character manipulation, including toUpper.
  2. main = do

    • Defines the main function, which handles input and output operations.
  3. putStrLn "Enter your name:"

    • Prints a prompt asking the user to enter a name.
  4. name <- getLine

    • Reads user input as a string and stores it in the name variable.
  5. putStrLn "Converted to upper case: "

    • Prints a message indicating that the string will be converted to uppercase.
  6. putStrLn $ map toUpper name

    • Uses map toUpper to convert each character in name to uppercase.
    • toUpper is a function from Data.Char that converts individual characters to uppercase.
    • map applies toUpper to each character in the string.
    • putStrLn then prints the modified string.

Output

$ ghc convert_toupper.hs
[1 of 1] Compiling Main             ( convert_toupper.hs, convert_toupper.o )
Linking convert_toupper ...
$ ./convert_toupper 
Enter your name:
oodlescoop
Converted to upper case: 
OODLESCOOP