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".
As Numeri suggests, removing the last occurrence by removing the first occurrence in the reversed list is one way:
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: