stringlistfunctionhaskellwinghci

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


Solution

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