[plt-scheme] looking for a pattern

From: Daniel Bastos (dbastos+0 at toledo.com)
Date: Thu Aug 27 18:57:05 EDT 2009

(*) The problem

I'd like to apply a series of regular expression search and replace on
a string. Here's an example of what I'm looking for.

> (f (f (f "mátémátiça" '("[á]" "a")) '("[é]" "e")) '("[ç]" "c"))

What pattern would this be?

(*) 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.

I think these are the only patterns I know. It can certainly be made
recursive, because of the example written above. So I wrote the
following to see if some light would come.

(define (conv table s)
    [(empty? table) s]
    [ else (regexp-replace* (car (car table)) 
                            (conv (cdr table) s) 
                            (last (car table)))]))

> (conv '(("[á]" "a") ("[é]" "e") ("[ç]" "c")) "mátémátiça")

Seems to work.

I also wrote the following trying to clean up the cars and cdrs.

(define (conv table s)
    [(empty? table) s]
    [ else 
       (letrec ((pat (car (car table)))
       	         (ins (last (car table))))
                (regexp-replace* pat (conv (cdr table) s) ins))]))

Doesn't seem much better.

Posted on the users mailing list.