Haskell程序:检查字母是元音还是辅音


我们可以使用Haskell中的`elem`函数来检查给定的字母是元音还是辅音。在第一个例子中,我们将使用`(isVowel c = c `elem` "aeiouAEIOU")`函数。在其他例子中,我们将结合使用`elem`函数和一些if-else语句、嵌套条件和守卫。

算法

  • 步骤1 - 定义数据类型’Person’,包含两个字段:姓名和年龄。

  • 步骤2 - 使用`elem`函数定义`isVowel`函数:

  • 步骤3 - 程序执行将从`main`函数开始。`main()`函数控制整个程序。它写成`main = do`。

  • 步骤4 - 使用if-else条件检查变量是元音还是辅音。然后将结果显示到控制台。

示例1

在这个例子中,使用`elem`函数检查字母是元音还是辅音。

Open Compiler
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"

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

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

示例2

在这个例子中,我们定义了一个函数`isVowel`,它接受一个字符作为输入,如果它是元音则返回`True`,否则返回`False`。然后,在`main`函数中,我们提示用户输入一个字符,并检查它是否是一个有效的字母。如果是,我们使用`isVowel`函数检查它是元音还是辅音,并打印相应的提示信息。如果输入无效,我们打印错误信息。

Open Compiler
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."

输出

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

示例3

在这个例子中,函数`isVowel`接收一个字符作为输入,如果字符是元音(a、e、i、o、u、A、E、I、O、U),则返回`True`,否则返回`False`。`elem`函数用于检查字符是否在元音列表中。

Open Compiler
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)

输出

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

示例4

在这个例子中,定义了一个函数`isVowel`,它接受一个字符作为输入,如果它是元音则返回`True`,否则返回`False`。`elem`函数用于检查字符是否在字符串"aeiouAEIOU"中,该字符串包含所有元音。

Open Compiler
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."

输出

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

示例5

在这个例子中,定义了一个函数`isVowel`,如果给定的字符是元音,则返回`True`,否则返回`False`。然后,`main`函数生成字母表,并使用`isVowel`过滤它以分离元音和辅音。最后,它打印这两个列表。

Open Compiler
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) ]

输出

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

Consonants: 
bcdfghjklmnpqrstvwxyz

结论

在英语中,元音是字母'a'、'e'、'i'、'o'、'u',有时还有'y'。辅音是字母表中的所有其他字母。我们可以通过在Haskell中使用模式匹配和元音列表来检查字母是元音还是辅音。我们还可以结合使用`elem`函数和各种if-else条件。

更新于:2023年3月28日

浏览量:371

开启你的职业生涯

完成课程获得认证

开始学习
广告