問題:翻譯成「豬拉丁文」的簡單規則是取一個以元音開頭並添加「yay」的單詞,同時接受任何與一個或多個輔音開頭並轉移的單詞他們在追加「唉」之前到後面。例如,「有能力」變成「可以」,「條帶」變成「可以」。編寫一個函數,將字符串轉換爲它的Pig-Latin翻譯。Haskell:豬拉丁文
實現:
-- define function to detect vowel
isVowel :: Char -> Bool
isVowel c = elem c ['u','e','o','a','i']
-- define function Latin Pig
lp ::String -> String
lp str = if (isVowel (head str)) then do {str ++ "yay"}
else
do {
str ++ (head str)
tail str
lp str
}
問題:到目前爲止,我沒有看到我的代碼(邏輯)的任何問題。老實說,這是我的Haskell入門課程的作業。然而,編譯器給我的錯誤:
**Couldn't match expected type `t0 -> t1 -> t2 -> t3 -> [Char]'
with actual type `Char'
Expected type: [t0 -> t1 -> t2 -> t3 -> [Char]]
Actual type: String
In the first argument of `head', namely `str'
In the second argument of `(++)', namely
`(head str) tail str lp str'
Failed, modules loaded: none.**
我的代碼有什麼問題?
'do'-notation是單子,我真的懷疑你*想要*在這裏使用List monad。 –
查理,我認爲你的問題在於你不懂基本的haskell語法。不要只是一個C程序,對它進行一些基本的轉換,並期望結果在Haskell中工作。 – Cubic