String haskell:删除列表中的特定字符串

String haskell:删除列表中的特定字符串,string,haskell,String,Haskell,我编写此函数是为了删除列表中以“{”开头并以“}”结尾的任何单词: delete []=[] delete c@(x:xs) | head x=='{' && last x=='}'= rep x " " (unwords c) | otherwise = delete xs 我正在使用助手函数rep: rep _ _ [] = [] rep a b s@(x:xs)= if isPrefixOf a s then

我编写此函数是为了删除列表中以“{”开头并以“}”结尾的任何单词:

delete []=[]
delete c@(x:xs) | head x=='{' && last x=='}'= rep x " " (unwords c)
                | otherwise = delete xs 
我正在使用助手函数rep:

rep _ _ [] = []
rep a b s@(x:xs)= if isPrefixOf a s
              then b++rep a b (drop(length a)s)
              else x: rep a b xs
当我对如下字符串列表调用delete函数时,它成功删除了第一个字符串“author”,但留下了另外两个字符串“reference”、“title”:

[“{author}”、“has”、“stated”、“that”、“{reference}”、“has”、“been”、“published”和“…”]

请告诉我我的功能中缺少了什么

谢谢,


Omar

在找到匹配项后,您不会在
delete
上重复出现,因此只会删除第一个匹配项。另外,我认为
x
总是
unwords(x:xs)
的前缀,所以我不确定您为什么要检查它。实际上,使用
unwords
进行此操作看起来已经很奇怪了。你不能把整件事写成
filter(这里的一些谓词)listOfStrings
?@chi你的评论再一次把我引向了解决方案。所以我使用了一个过滤器:过滤器(\x->headx=/'{')我的列表。它解决了我的问题。谢谢你的帮助!但是请记住,
head
在使用空字符串时会失败,所以你最好在使用head之前检查空字符串。没错!这是空列表中head的错误。这是你正在检查的字符串的外部列表,但字符串本身就是列表(正如你所知道的,因为你在他们身上使用了
head
last
)。