hpaste

recent | annotate | new

import Data.List;
-- substring replacement
-- if the string is empty, don't bother.
replaceInString _ _ [] = [];
-- replace all occurances of s1 with s2.
replaceString s1 s2 str@ (x:xs) = do {
if s1 `isPrefixOf` str then
s2 ++ (replaceString s1 s2 (drop (length s1) str))
else
x:(replaceString s1 s2 xs);
};

module Main where {
import Data.List;
-- substring replacement
-- if the string is empty, don't bother.
replaceInString _ _ [] = [];
-- replace all occurances of s1 with s2.
replaceString s1 s2 str@ (x:xs) =
if s1 `isPrefixOf` str then 
s2 ++ (replaceString s1 s2 (drop (length s1) str)) 
else
x:(replaceString s1 s2 xs);
}

import Data.List

replaceString s1 s2 str =
    let n = length s1
        replaceString' [] = []
        replaceString' str@(x:xs)
            | s1 `isPrefixOf` str = s2 ++ replaceString' (drop n str)
            | otherwise           = x : replaceString' xs
    in replaceString' str

import Data.List;

replaceString s1 s2 str =
    let {n = length s1;
         replaceString' [] = [];
         replaceString' str@(x:xs)
            | s1 `isPrefixOf` str = s2 ++ replaceString' (drop n str)
            | otherwise           = x : replaceString' xs }
    in replaceString' str