[plt-scheme] Small problem with regex-replace*
From: Jaime Vargas (jev at mac.com)
Date: Thu Jun 15 23:39:44 EDT 2006 |
|
I wrote a small program that scrambles text by preserving the first
and last char of each word. There is a claim that such algorithm for
scrambling can produce readable text, because the human brain does
the error correction.
I was happy this was a very small program to write, however I wasn't
able to create a regexp that only match words,
so I ended using *dummy* variable to hold the second match that is
passed to the scramble procedure. How can I fix this? Here is the code:
(require (lib "cards.ss" "games" "cards"))
(define (shuffle-string str)
(list->string (shuffle-list (string->list str) 7)))
(define (scramble str dummy)
(define len (string-length str))
(define len-1 (sub1 len))
(string-append
(substring str 0 1) ; First char
(shuffle-string (substring str 1 len-1)) ; Shuffle mid-part
(substring str len-1 len))) ; Last char
(define (scramble-text str)
(let ([word (regexp "([a-zA-Z])+")])
(regexp-replace* word str scramble)))
(scramble-text "In theory after scrambling this text would be still
readable.")
---> "In theory afetr scrmlinbag tihs text would be sltil rabdelae."
Thanks,
Jaime