這是我第一次在Haskell中使用數據類型。 有問題,我不知道如何改進代碼。Haskell - 聲明/使用數據
這裏的問題是:
Declear稱爲「聲母」,其中包括一些字母(字符串)數據類型,並得到了有關如果在字中的所有字母都是輔音與否的信息的textstring(串),然後編寫一個函數「Cheak」,它獲得了indata(一些字母爲cheak)和outdata(數據類型爲「Consonant」)。
這裏是我的代碼:
module Consonant where
import Char
type Name = String
type ConOrNot = String
data Consonant = Cons Name ConOrNot
deriving (Show,Eq)
isVowel = "AEIOU"
cheak :: String -> Consonant
cheak [] = ""
cheak (char:chars) =
if elem (toUpper char) isVowel == false
then cheak chars
else cheak = Cons (char:chars) "Not Consonant"
-- here I want to use "break", but I don't know how to use it in Haskell...
cheak = Cons (char:chars) "Is Consonant"
它不工作...如何更改代碼?請幫忙!謝謝!
更新:
module Consonant where
import Char
type Word = String
type ConOrNot = String
data Consonant = Cons Word ConOrNot
deriving (Show,Eq)
isConsonant = "BCDFGHJKLMNPQRSTVWXYZ"
cheak :: String -> Consonant
cheak [] = Cons "" ""
cheak (char:chars)
|elem (toUpper char) isCosonant = cheak chars --if all the letters are cosonant, I want it return (Cons (char:chars) "is Consonant").. still working on it
|otherwise = Cons (char:chars) "Not Consonant"
它現在如果字符串有兩個元音輔音和或僅元音,如何提高代碼,以便它也只有輔音的作品?
這正是我想要的。感謝您的幫助。 – Ferry