I am working on this function which basically takes two arguments. The first one is
a number and the second is a list. I want to replace the first argument with 3 every single time I see it in my list. My function works fine. Here it is:
censorword _ [] = []
censorword b (x:xs)
| b == x = 3:censorword b xs
| otherwise = x:censorword b xs
My question is, how do I make it work for Strings. In other words, i would like to do something like this: censorword “ab” [“cdZ”,ab”] = [“cdZ”,”hello”] . Here, I have replaced “ab” with hello.
Appreciate any ideas.
You only need to change the value being replaced (
3in your original code).This function can be generalized and simplified with
map: