[英]Haskell recursively removing chars using helper function
I have a function defined as follows: 我有一个定义如下的函数:
rmCharsRec :: String -> String -> String
I have a helper function rmChar defined as follows that removes all occurrences of a character in a string: 我有一个定义如下的辅助函数rmChar,该函数删除字符串中所有出现的字符:
rmChar :: Char -> String -> String
rmCharsRec
should remove all the occurrences of each char in the first string, in the second string and return the response. rmCharsRec
应该删除第一个字符串,第二个字符串中每个char的所有出现,并返回响应。
I have tried the following: 我尝试了以下方法:
rmCharsRec :: String -> String -> String
rmCharsRec _ "" = ""
rmCharsRec c:chars str = rmChar ( c str ) ++ rmCharsRec chars
But it seems is not working. 但似乎不起作用。 I always get the following error:
我总是收到以下错误:
file.hs:140:1: Parse error in pattern: rmCharsRec
**This is not a homework problem*. **这不是作业问题*。 I just want to learn Haskell from a set of exercises, and I got stuck to this as I don't get why I get an error.
我只想通过一系列练习来学习Haskell,但由于无法理解为什么会出现错误,所以我坚持了这一点。
Can somebody please notify me about what's the problem with my code? 有人可以告诉我我的代码有什么问题吗?
If you want to write a pattern like c:chars
you need to put in it brackets like so: 如果要编写类似
c:chars
的模式,则需要将其放在方括号中,如下所示:
rmCharsRec (c:chars) str = ...
That will resolve the error message you mention, although I think that that's not the only issue you'll need to deal with. 这将解决您提到的错误消息,尽管我认为这不是您需要处理的唯一问题。
您忘记了在模式匹配中在c:chars
周围设置括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.