import Data.List (tails, elemIndices, isPrefixOf)

replace :: String -> String -> String -> String
replace [] _ xs = xs
replace _ [] xs = xs
replace _ _ []  = []
replace a b xs  = replAll
    where
        
        xtails = tails xs
        
        
        matches = elemIndices True $ map (isPrefixOf a) xtails
        
        repl ys n = take n ys ++ b ++ drop (n + length b) ys
        
        replAll = foldl repl xs matches

replaceInFiles a1 a2 files = do
    f <- mapM readFile files
    return $ map (replace a1 a2) f