[plt-scheme] Small problem with regex-replace*

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri Jun 16 08:14:53 EDT 2006

On Jun 15, 2006, at 11:39 PM, Jaime Vargas wrote:

> 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 have seen such a claim before, as a joke. Do you have a URL handy? -- 

P.S. This might be a good 101 assignment.

> 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
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.