Haskell Program to convert string to lower case

Program

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

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

  1. import Data.Char

    • Imports the Data.Char module, which provides character-related functions like toLower.
  2. main = do

    • Defines the main function for handling 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 lower case: "

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

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

Output

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