Logo

Programming-Idioms

History of Idiom 63 > diff from v14 to v15

Edit summary for version 15 by :

Version 14

2015-09-03, 21:01:05

Version 15

2015-09-05, 10:19:40

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
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