【发布时间】:2013-04-29 07:15:27
【问题描述】:
我想以这样的 2 个列表为例;
find=["Hou","House","Mouse"]
repl=["Mou","Bird","House"]
所以当我给出这样的文字时;
"The House with Mouse is big"
输出应该是这样的;
"The Mouse with House is big"
所以我写了这个;
replace :: String->String->String->String
replace _ _ []=[]
replace find repl text
= if take(length find) text == find
then repl ++ replace find repl (drop (length find) text)
else [head text] ++ (replace find repl (tail text))
replaceMore ::[String]->[String]->String->String
replaceMore _ _ []=[]
replaceMore _ [] _ =[]
replaceMore [] _ _ =[]
replaceMore find repl text
= if (tail find) == [] || (tail repl)==[]
then text
else replaceMore (tail find)
(tail repl)
(replace (head find) (head repl) text)
返回
"The Mouse with Mouse is big"
所以它不像我想要的那样工作,我认为问题就在这里;
replaceMore _ _ []=[]
replaceMore _ [] _ =[]
replaceMore [] _ _ =[]
但我仍然不知道如何解决这个问题。所以有什么想法吗?
【问题讨论】:
-
让人们轻松地帮助你。不要只说“它不起作用”:说如何它不起作用工作。 如果有错误消息, 将它们复制并粘贴到您的问题中。 如果代码运行但给出了错误的答案,然后给出以下示例:输入您的代码,错误的输出您的代码返回,正确的输出您希望您的代码返回。
-
不,您的问题不在您认为的位置。整个想法是错误的。你不能用
replace构建replaceMore。您需要并行搜索所有模式,并替换找到的第一个。
标签: string list haskell replace