我有一个作业,我必须在Haskell的给定句子中更改和省略某些单词。例如:
输入:
Give me the nippers, please!
输出:
Giv me the tippers!
and here is my attempt:
replaceWord [] = []
replaceWord(x:xs)
|x == "Give" = "Giv" : replaceWord xs
|x == "me" = "mi" : replaceWord xs
|otherwise = x : replaceWord xs
where (x:xs) = words (x:xs)
I became an error message saying:
"Couldn't match type ‘[Char]’ with ‘Char’
Expected type: [Char]
Actual type: [String] • In the expression: words (x : xs)
In a pattern binding: (x : xs) = words (x : xs)
In an equation for ‘replaceWord’:
replaceWord (x : xs)
| x == "Geben" = "Gib" : replaceWord xs
| x == "Sie" = "mi" : replaceWord xs
| otherwise = x : replaceWord
where
(x : xs) = words (x : xs)
|
9 | where (x:xs) = words (x:xs) | "
I can not figure out how to use pattern match and how to change a string in to a list made of words. I have to use recursion and pattern matching and not allowed to use list functions such as map, filter, concat, etc.
Any suggestions?