Computing all possibilities of replacing one character by another

Viewed 336
> magicFunction 'l' '_' "hello world"
["he_lo world", "hel_o world", "hello wor_d"]

Is there such a magic function in the standard Prelude or can it be composed easily with other functions?

And no, this isn't homework, but still, please don't spend too much time hand-rolling your own complicated solution, I'd rather do that myself than waste your time ;) Just asking if it's in the standard.


EDIT: Here is my first attempt:

import Data.List (findIndices)

replace i y xs = take i xs ++ y : drop (i+1) xs

magicFunction x y xs = map (\i -> replace i y xs) (findIndices (== x) xs)

Can it be improved? Surely something like replace must be in the standard? I found replace :: Eq a => a -> a -> [a] -> [a] in Network.CGI.Protocol, but it has the wrong signature.

2 Answers
Related