Haskell Program to Check Whether a Character is Alphabet or Not


In Haskell, we will Check Whether a Character is Alphabet or Not by using relational operator comparison, if-else statements and Data. Char module. In the first example, we are going to use (isAlpha c | c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' = "Alphabet" | otherwise = "Not an Alphabet") function. And in the second example, we are going to use (isAlpha c = if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' then "Alphabet" else "Not an Alphabet") function. Where as in third example, we are going to use Data.Char module.

Algorithm

  • Step 1 − The isAlpha function is defined as,

  • For example 1 −

isAlpha c
| c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' = "Alphabet"
| otherwise = "Not an Alphabet".
  • For example 2 −

isAlpha c = if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
              then "Alphabet"
              else "Not an Alphabet".
  • Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. The main function is used to call the isAlpha function with a test character 'a'. The result is printed to the console using the putStrLn function.

  • Step 3 − The variable named, “c” is being initialized. It will hold the character which is to be checked whether it is alphabet or not.

  • Step 4 − The result is printed to the console using ‘putStrLn’ after the function is called.

Example 1

In the following example, the isAlpha function takes a character c as an argument and checks if it falls within the range of 'a' to 'z' or 'A' to 'Z'. If it does, the function returns the string "Alphabet". If not, it returns the string "Not an Alphabet".

isAlpha :: Char -> String
isAlpha c
   | c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' = "Alphabet"
   | otherwise = "Not an Alphabet"

main :: IO ()
main = do
   let c = 'a'
   putStrLn (isAlpha c)

Output

Alphabet

Example 2

In following example, the isAlpha function uses an if expression to check whether the given character c falls within the range of 'a' to 'z' or 'A' to 'Z'. If it does, the function returns the string "Alphabet", otherwise it returns the string "Not an Alphabet".

isAlpha :: Char -> String
isAlpha c = if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
               then "Alphabet"
            else "Not an Alphabet"

main :: IO ()
main = do
   let c = 'a'
   putStrLn (isAlpha c)

Output

Alphabet

Example 3

In this example, we import the Data.Char module, which provides several useful functions for working with characters. One of these functions is isAlpha, which checks if a character is an alphabet.

import Data.Char (isAlpha)

main :: IO ()
main = do
   let c = 'a'
   putStrLn (if isAlpha c then "Alphabet" else "Not an Alphabet")

Output

Alphabet

Conclusion

In Haskell, alphabet characters are characters that represent the letters of an alphabet, such as the basic Latin alphabet ('a' to 'z' and 'A' to 'Z') or other scripts like Greek, Cyrillic, or Chinese. We can use relational operators, if-else statements and in-built function to check whether the character passed is alphabet or not.

Updated on: 27-Mar-2023

499 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements