[英]Haskell function that takes out last occurrence of input character
I'm having trouble writing this function that takes a character and a list of characters, then eliminates the last occurrence of that input character in the list. 我在编写这个带有字符和字符列表的函数时遇到了麻烦,然后消除了列表中最后一次出现的输入字符。 I was able to take out the first occurrence of the input character with my function below: 我可以使用下面的函数取出第一次出现的输入字符:
fun :: Char -> String -> String
fun c (s:ss)
| s == c = ss
| otherwise = s : fun c ss
fun _ [] = []
What I need help on is how I should modify this function to take out the last occurrence of the input character, instead of the first. 我需要帮助的是如何修改此函数以取出输入字符的最后一次出现,而不是第一次出现。 The result should be something like fun 'c' "abcdccytrc"
returning "abcdccytr"
. 结果应该是fun 'c' "abcdccytrc"
返回"abcdccytr"
。
Okay, here's what I came up with: 好的,这就是我想出的:
fun :: Char -> String -> String
fun c (s:ss)
| ((fun c ss) == ss) && (s == c) = ss
| otherwise = s : fun c ss
fun _ [] = []
Essentially, if s == c
, and the rest of the string ( ss
) is unchanged by running this function on it (ie, it contains no character c
), then return the remaining characters. 本质上,如果s == c
,并且其余的字符串( ss
)通过在其上运行此函数而保持不变(即,它不包含字符c
),则返回剩余的字符。
If this requirement isn't met (ie, the rest of the string has the character c
at least one time), retain the current character and apply the function to the rest of the string. 如果不满足此要求(即,字符串的其余部分至少有一次字符c
),请保留当前字符并将该函数应用于字符串的其余部分。
Aside from this, I think reversing the string and then calling your original function, then reversing it again, like I suggested in a comment, might be more understandable, but that's just opinion. 除此之外,我认为反转字符串然后调用你的原始函数,然后再次反转它,就像我在评论中建议的那样,可能更容易理解,但那只是意见。
As Numeri suggests, removing the last occurrence by removing the first occurrence in the reversed list is one way: 正如Numeri建议的那样,通过删除反向列表中的第一个匹配项来删除最后一个匹配项是一种方法:
removeFirst :: Char -> String -> String
removeFirst _ [] = []
removeFirst c1 (c2:cs) = if c1 == c2 then cs else c2:removeFirst c1 cs
removeLast :: Char -> String -> String
removeLast c1 = reverse . removeFirst c1 . reverse
As Will Ness suggests, returning the string in which the last occurrence is removed, and a boolean to indicate whether the current occurrence should be removed or not, is another: 正如Will Ness建议的那样,返回删除最后一个匹配项的字符串,以及指示是否应该删除当前事件的布尔值是另一个:
removeLast :: Char -> String -> String
removeLast c1 = snd . remLast
where
remLast :: String -> (Bool, String)
remLast [] = (False, [])
remLast (c2:cs) =
case remLast cs of
(True, cs') -> (True, c2:cs')
(False, cs') -> if c1 == c2 then (True, cs') else (False, c2:cs')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.