- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Java Program to Check Whether a Character is Alphabet or Not
- C++ Program to Check Whether a Character is Alphabet or Not
- Haskell Program to Check Whether an Alphabet is Vowel or Consonant
- How to check whether a character is in the Alphabet or not in Golang?
- Java program to find whether the given character is an alphabet or not
- Haskell Program to check whether a variable is defined or not
- Haskell Program to Check Whether a Number is Prime or Not
- Java Program to Check Whether an Alphabet is Vowel or Consonant
- Golang Program to Check Whether an Alphabet is Vowel or Consonant
- Check whether a character is Lowercase or not in Java
- Check whether a character is Uppercase or not in Java
- Haskell Program to Check Whether a Number is Even or Odd
- Haskell Program to Check Whether a Number is Positive or Negative
- C++ Program to Check Whether a character is Vowel or Consonant
- Check input character is alphabet, digit or special character in C
