Haskell Program to Count the Number of Vowels and Consonants in a Sentence


In this tutorial, we discuss writing a program to count the number of vowels and consonants in a Sentence in the Haskell programming language. Haskell is a Declarative, Strongly Typed, and Functional programming language. The computations in Haskell are mathematical functions.

Vowels are the open sound-producing letters in English literature. The list of vowels in the English alphabet is ‘a’, ’e’, ’i’, ’o’, and ’u’. The other letters are consonants.

In this tutorial, we see two different ways to implement the program to count the number of vowels and consonants in Haskell.

  • Implementing the program to count the number of vowels and consonants using if-else statements.

  • Implementing the program to count the number of vowels and consonants using the list function elem with if-else.

Algorithm steps

  • Declare or take input a sentence.

  • Implement the program to compute the count of vowels and consonants in string.

  • Print or Display the computed counts.

Example 1

Implementing the program to count the number of vowels and consonants using if-else statements

import Data.Char -- function declaration for countVowels countVowels::[Char]->Int -- function definition for countVowels -- base case countVowels [] = 0 countVowels (ch:str) = if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') then 1 + (countVowels str) else (countVowels str) -- function declaration for countConsonants countConsonants::[Char]->Int -- function definition for countConsonants -- base case countConsonants [] = 0 countConsonants (ch:str) = if (((ord ch)>=97 && (ord ch)<=122) && not (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')) then 1 + (countConsonants str) else (countConsonants str) main :: IO() main = do -- declaring and initializing the variable for the sentence let input = "Thank you for visiting Tutorialspoint =" -- case conversion of the input string to lowercase let sentence = map toLower input -- invoking the function countVowels with the argument sentence and printing the returned result print ("The number of vowels in the sentence: "++ input) print (countVowels sentence) print ("The number of consonants in the sentence: "++ input) print (countConsonants sentence)

Output

"The number of vowels in the sentence: Thank you for visiting Tutorialspoint ="
13"The number of consonants in the sentence: Thank you for visiting Tutorialspoint ="
20

In the above program,

  • We initially imported the Chat module from the package, which contains utility functions on the Character data type.

  • We declared a function countVowels as such it takes a string as an argument and returns an integer. In its function definition, we are accepting the string argument using pattern matching (string destructuring). We are accessing string as a combination of its first element and other elements. Then we check the first element, if the first element is a vowel then we are returning a recursive call with the remaining string as an argument addition with value 1. Else we are just returning the recursive call with the remaining string as an argument. I.e The recursive calls are invoked until the function attains base case where the string is empty where the function returns 0.

  • The countVowels function returns the count of vowels in a string.

  • We declared a function countConsonants as such it takes a string as an argument and returns an integer. In its function definition, we are accessing the head element and tail of the string using pattern matching. We are checking the character is a letter and not a vowel. If the condition is satisfied, we are returning the recursive call with the remaining string as argument addition with value 1. Else we are just returning the function call. The recursive calls are invoked until the function reaches the base case where the string argument is empty where the function returns 0;

  • The countConsonants function returns the count of consonants in a string.

  • Finally, we are invoking these functions from the main and printing the returned result.

Note − ord is a function that returns the ASCII value of a character, this is a utility function available in the Char module.

Example 2

Implementing the program to count the number of vowels and consonants using the list function elem.

import Data.Char -- function declaration for countVowels countVowels::[Char]->Int -- function definition for countVowels -- base case countVowels [] = 0 countVowels (ch:str) = if (elem ch ['a','e','i','o','u']) then 1 + (countVowels str) else (countVowels str) -- function declaration for countConsonants countConsonants::[Char]->Int -- function definition for countConsonants -- base case countConsonants [] = 0 countConsonants (ch:str) = if (elem ch (map chr [97..122]))&& not (elem ch ['a','e','i','o','u']) then 1 + (countConsonants str) else (countConsonants str) main :: IO() main = do -- declaring and initializing variable for sentence let input = "Thank you for visiting Tutorialspoint" -- case conversion of input string to lowercase let sentence = map toLower input -- invoking the function countVowels with argument sentence and printing the returned result print ("The number of vowels in the sentence:"++ input) print (countVowels sentence) print ("The number of consonants in the sentence:"++ input) print (countConsonants sentence)

Output

"The number of vowels in the sentence:Thank you for visiting Tutorialspoint"
13
"The number of consonants in the sentence:Thank you for visiting Tutorialspoint"
20

In the above program,

  • elem is a function that takes a single element and a list of element as argument chect the existence of the element in the list. If the element is present in the list then the function returns true, Else it returns false.

  • In the function countVowels. We are checking the first element with the list [‘a’,’e’,’i’,’o’,’u’] using the function elem. And rest is the same as in the function in Example1.

  • The countVowels function returns the count of vowels in a string.

  • In the function countConsonants, we are generating all the lowercase characters using the map function to check whether the element is a character and the rest is the same as in the previous Example.

  • The countConsonants function returns the count of consonants in a string.

  • Finally, we are invoking these functions from the main and printing the returned result.

Note − char is a function that returns a character for an ASCII value, this is a utility function available in the Char module.

Conclusion

In this tutorial, we discussed writing a program to count the number of vowels and consonants in a Sentence in Haskell programming language.

Updated on: 24-Nov-2022

465 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements