Haskell程序:计算句子中元音和辅音的数量


本教程将讨论如何编写一个Haskell程序来计算句子中元音和辅音的数量。Haskell是一种声明式、强类型和函数式编程语言。Haskell中的计算是数学函数。

元音是英语文学中产生开放音的字母。英语字母表中的元音字母是“a”、“e”、“i”、“o”和“u”。其他字母是辅音。

在本教程中,我们将看到两种不同的方法来实现Haskell程序,以计算元音和辅音的数量。

  • 使用if-else语句实现计算元音和辅音数量的程序。

  • 使用列表函数elem和if-else语句实现计算元音和辅音数量的程序。

算法步骤

  • 声明或输入句子。

  • 实现程序以计算字符串中元音和辅音的数量。

  • 打印或显示计算结果。

示例1

使用if-else语句实现计算元音和辅音数量的程序

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)

输出

"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

在上面的程序中:

  • 我们首先从包中导入了Chat模块,该模块包含Character数据类型的实用函数。

  • 我们声明了一个名为countVowels的函数,它接受一个字符串作为参数并返回一个整数。在其函数定义中,我们使用模式匹配(字符串解构)接受字符串参数。我们访问字符串作为其第一个元素和其他元素的组合。然后我们检查第一个元素,如果第一个元素是元音,则我们使用剩余字符串作为参数加1进行递归调用。否则,我们只使用剩余字符串作为参数进行递归调用。即递归调用将一直进行,直到函数达到基例(字符串为空),函数返回0。

  • countVowels函数返回字符串中元音的数量。

  • 我们声明了一个名为countConsonants的函数,它接受一个字符串作为参数并返回一个整数。在其函数定义中,我们使用模式匹配访问字符串的首元素和尾元素。我们检查字符是否是字母且不是元音。如果条件满足,则我们使用剩余字符串作为参数加1进行递归调用。否则,我们只返回函数调用。递归调用将一直进行,直到函数达到基例(字符串参数为空),函数返回0。

  • countConsonants函数返回字符串中辅音的数量。

  • 最后,我们在main函数中调用这些函数并打印返回的结果。

注意 - ord是一个返回字符ASCII值的函数,这是Char模块中可用的实用函数。

示例2

使用列表函数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)

输出

"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

在上面的程序中:

  • elem是一个函数,它接受一个单个元素和一个元素列表作为参数,检查元素在列表中是否存在。如果元素存在于列表中,则函数返回true,否则返回false。

  • countVowels函数中,我们使用函数elem检查第一个元素与列表['a','e','i','o','u']。其余部分与示例1中的函数相同。

  • countVowels函数返回字符串中元音的数量。

  • countConsonants函数中,我们使用map函数生成所有小写字符,以检查元素是否为字符,其余部分与前面的示例相同。

  • countConsonants函数返回字符串中辅音的数量。

  • 最后,我们在main函数中调用这些函数并打印返回的结果。

注意 - char是一个返回ASCII值对应字符的函数,这是Char模块中可用的实用函数。

结论

在本教程中,我们讨论了如何编写一个Haskell程序来计算句子中元音和辅音的数量。

更新于:2022年11月24日

浏览量:578

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.