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.
-
import Data.Char
- Imports the
Data.Char
module, which provides character-related functions liketoLower
.
- Imports the
-
main = do
- Defines the
main
function for handling input and output operations.
- Defines the
-
putStrLn "Enter your name:"
- Prints a prompt asking the user to enter a name.
-
name <- getLine
- Reads user input as a string and stores it in the
name
variable.
- Reads user input as a string and stores it in the
-
putStrLn "Converted to lower case: "
- Prints a message indicating that the string will be converted to lowercase.
-
putStrLn $ map toLower name
- Uses
map toLower
to convert each character inname
to lowercase. toLower
is a function fromData.Char
that converts individual characters to lowercase.map
appliestoLower
to each character in the string.putStrLn
then prints the modified string.
- Uses
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