Haskell function that takes out last occurrence of input character

一世执手 提交于 2019-12-23 21:07:23

问题


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".


回答1:


As Numeri suggests, removing the last occurrence by removing the first occurrence in the reversed list is one way:

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:

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')



回答2:


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 (i.e., it contains no character c), then return the remaining characters.

If this requirement isn't met (i.e., 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.

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.



来源:https://stackoverflow.com/questions/33683079/haskell-function-that-takes-out-last-occurrence-of-input-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!