I am looking for a way to replace occurrences of strings in Haskell. I have this working for single pairs. My current function is implemented like so:
replaceList :: (Eq a) => [a] -> ([a],[a]) -> [a]
replaceList z@(x:xs) (a,b)
| take (length a) z == a = b ++ strt z (length a)
| otherwise = x : replaceList xs (a,b)
In this context, strt is just a function that returns a list starting at a certain index. This function works. replaceList "Dragon" ("ragon","odo") will return "I am a dodo". However, I am looking for a way to make this function accept a list of these tupples. For instance:
replaceList "I am a dragon" [("I","You"),("am","are"),("a dragon","awesome")] returning "You are awesome".
The methods I have tried so far have been to map a partially applied replaceList over a list of tupples, but that returns a list of each individual element changed. I also tried using this code:
replaceInfinity :: (Eq a) => [a] -> [([a],[a])] -> [a]
replaceInfinity x [] = x
replaceInfinity x ((a,b):ys) = (flip replaceList (a,b)) . (replaceInfinity x ys)
but that fails to compile. I'm relatively new to Haskell and am using this to rewrite an old language preprocessor. I can't understand the logic of how to implement this type of function.
Could someone tell me the tactic to get to the answer, or even implement the function for me so I could learn how it would be done? If it helps, I have the entire source file I've been using to play around with this here: http://hpaste.org/85363