[plt-scheme] looking for a pattern
> #lang scheme
>
> (require test-engine/scheme-tests)
>
> ;;
> -----------------------------------------------------------------------------
> (check-expect (f (f (f "mátémátiça" '("[á]" "a")) '("[é]" "e"))
> '("[ç]" "c"))
> "matematica")
>
> (define (f str from-to)
> (regexp-replace* (first from-to) str (second from-to)))
>
> ;;
> -----------------------------------------------------------------------------
>
> (check-expect (F "mátémátiça" '(("[á]" "a") ("[é]" "e") ("[ç]" "c")))
> "matematica")
>
> (define (F str from-to-table)
> (foldl (lambda (from-to str) (f str from-to)) str from-to-table))
>
> (define (tee f)
> (lambda x
> (printf "~s -> ~s\n" x (apply f x))
> (apply f x)))
>
> (test)
On Aug 27, 2009, at 6:57 PM, Daniel Bastos wrote:
> (*) 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"))
> "matematica"
>
> 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)
> (cond
> [(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")
> "matematica"
>
> Seems to work.
>
> I also wrote the following trying to clean up the cars and cdrs.
>
> (define (conv table s)
> (cond
> [(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.
>
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme