[plt-scheme] looking for a pattern

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Aug 27 21:30:23 EDT 2009

On Aug 27, Daniel Bastos wrote:
> (*) Investigations
> At first I thought this would be a fold. It really looks like it,
> but I'm not really sure because I tend to think of folds on f
> requiring f to be a binary operator, and my operands have different
> types. But maybe I'm wrong about folds. It's not a map because I
> must use the result of the first f-application, in the second.

Folding works fine for this:

  > (define patterns '(["A" "B"] ["C" "D"]))
  > (foldl (lambda (subst str)
             (regexp-replace* (car subst) str (cadr subst)))
  > (for/fold ([str "ABCDEF"]) ([subst patterns])
      (regexp-replace* (car subst) str (cadr subst)))

But one thing that can lead to a very different solution is the fact
that the substitutions are not independent -- in that case you'd want
to create a pattern that matches all of them and arrange for a
replacement in a single call.  But this can be a little tricky to do.

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.