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程式來計算句子中母音和子音的數量。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP