質問:「PigLatin」に翻訳するための簡単なルールは、母音で始まる単語を取り、「yay」を追加し、1つ以上の子音で始まる単語を取り、それらを後ろに転送してから「 ay」。たとえば、「able」は「ableyay」になり、「stripe」は「ipestray」になります。文字列をPig-Latin翻訳に変換する関数を記述します。
実装:
-- 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.**
私のコードの何が問題になっていますか?!