Haskell Program to Check Whether an Alphabet is Vowel or Consonant


We can use elem function in Haskell to check whether the given alphabet is vowel or consonant. In the first example, we are going to use (isVowel c = c `elem` "aeiouAEIOU") function. And in other examples, we’re going to use elem function along with certain if-else statements, nested conditions and guards.

Algorithm

  • Step 1 − The data type ‘Person’ is defined with two fields name and an age.

  • Step 2 − The isVowel function is defined using elem function as

  • Step 3 − The program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 4 − The if-else condition is used to check if a variable is vowel or consonant. Then resultant answer is displayed to the console.

Example 1

In this example, an alphabet is checked whether it is a vowel or consonant using elem function.

isVowel :: Char -> Bool
isVowel c = c `elem` "aeiouAEIOU"
main :: IO ()
main = do
   let alphabet = 'a'
   if isVowel alphabet
      then putStrLn $ alphabet : " is a vowel"
      else putStrLn $ alphabet : " is a consonant"

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
a is a vowel

Example 2

In this example, we define a function isVowel that takes a character and returns True if it is a vowel and False otherwise. Then, in the main function, we prompt the user to enter a character and check if it is a valid alphabet. If it is, we check if it is a vowel or consonant using the isVowel function and print the appropriate message. If the input is invalid, we print an error message.

isVowel :: Char -> Bool
isVowel c = c `elem` "aeiouAEIOU"
main :: IO ()
main = do
   let alphabets ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
   let c = 'a'
   if c `elem` alphabets
      then if isVowel c
      then putStrLn $ c : " is a vowel."
      else putStrLn $ c : " is a consonant."
   else putStrLn "Invalid input."

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
a is a vowel.

Example 3

In this example, the function isVowel takes a Char as input and returns True if the character is a vowel (a, e, i, o, u, A, E, I, O, U) and False otherwise. The elem function is used to check if the character is in the list of vowels.

isVowel :: Char -> Bool
isVowel c = c `elem` "aeiouAEIOU"

main :: IO ()
main = do
  let c1 = 'a'
      c2 = 'b'
  putStrLn $ "Is " ++ [c1] ++ " a vowel? " ++ show (isVowel c1)
  putStrLn $ "Is " ++ [c2] ++ " a vowel? " ++ show (isVowel c2)

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
Is a a vowel? True
Is b a vowel? False

Example 4

In this example, a function isVowel is defined that takes a character and returns True if it is a vowel and False otherwise. The elem function is used to check if the character is in the string "aeiouAEIOU", which contains all the vowels.

isVowel :: Char -> Bool
isVowel c = c `elem` "aeiouAEIOU"

main :: IO ()
main = do
  let char1 = 'a'
      char2 = 'b'
  putStrLn $ char1 : if isVowel char1 then " is a vowel." else " is a consonant."
  putStrLn $ char2 : if isVowel char2 then " is a vowel." else " is a consonant."

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
a is a vowel.
b is a consonant.

Example 5

In this example, a function isVowel is defined which returns True if the given character is a vowel, and False otherwise. The main function then generates the alphabet and filters it using isVowel to separate vowels and consonants. Finally, it prints the two lists.

isVowel :: Char -> Bool
isVowel c
  | c `elem` "aeiouAEIOU" = True
  | otherwise            = False

main :: IO ()
main = do
  let alphabet = "abcdefghijklmnopqrstuvwxyz"
  putStrLn "Vowels: "
  putStrLn [ c | c <- alphabet, isVowel c ]
  putStrLn "
Consonants: " putStrLn [ c | c <- alphabet, not (isVowel c) ]

Output

Vowels: 
[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
Vowels: 
aeiou

Consonants: 
bcdfghjklmnpqrstvwxyz

Conclusion

In English language, vowels are the letters 'a', 'e', 'i', 'o', 'u', and sometimes 'y'. Consonants are all the other letters of the alphabet. We can check if an alphabet is a vowel or consonant in Haskell by using pattern matching and a list of vowels. We can also use elem function with various if-else conditions.

Updated on: 28-Mar-2023

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements