Logo

Programming-Idioms

History of Idiom 63 > diff from v15 to v16

Edit summary for version 16 by :

Version 15

2015-09-05, 10:19:40

Version 16

2015-09-05, 10:21:30

Idiom #63 Replace fragment of a string

Assign to x2 the value of string x with all occurrences of y replaced by z.
Assume occurrences of y are not overlapping.

Idiom #63 Replace fragment of a string

Assign to x2 the value of string x with all occurrences of y replaced by z.
Assume occurrences of y are not overlapping.

Imports
import Data.List
Imports
import Data.List
Code
replacing input pattern replacement = if isPrefixOf pattern input
 then replacement ++ replacing (drop (length from) input) pattern replacement
 else if null input then [] else (head input) : replacing (tail input) pattern replacement

x2 = replacing x y z
Code
replacing input pattern replacement =
 if isPrefixOf pattern input
 then replacement ++ 
        replacing (drop (length from) input) pattern replacement
 else if null input then []
 else (head input) : replacing (tail input) pattern replacement

x2 = replacing x y z