[Haskell-cafe] trivial function application question

J. Garrett Morris trevion at gmail.com
Thu Jan 4 16:50:12 EST 2007


On 1/4/07, brad clawsie <clawsie at fastmail.fm> wrote:
> lets say i have a string
>
> s = "abcdefg"
>
> now i have two lists of strings, one a list of patterns to match, and
> a list of replacement strings:
>
> patterns = ["a","b"]
> replace = ["Z","Y"]
>
> from which my intent is that "a" be replaced by "Z", "b" by "Y" etc
>
> now using the replace function from MissingH.Str (which i know is now
> renamed), i wish to apply replace to s using (pattern[0], replace[0]),
> (pattern[1], replace[1])...(pattern[N], replace[N]).

You can create the replacing functions using zipWith :: (a -> b -> c)
-> [a] -> [b] -> [c] (from the Prelude) as follows:

replacers = zipWith patterns replace

You then need to apply these functions to your starting string s.  I
would probably use foldr for that, something like this:

foldr ($) s replacers

Where ($) performs function application.

As Neil points out, if your replacements overlap, this could cause
replacement text to itself be replaced.

 /g

-- 
It is myself I have never met, whose face is pasted on the underside of my mind.


More information about the Haskell-Cafe mailing list