[racket] For new Racket users

From: Stephen Bloch (bloch at adelphi.edu)
Date: Sun Aug 28 07:23:10 EDT 2011

> Eli Barzilay <eli at ...> writes:
>> (define (translate str)
>>  (define A (char->integer #\a))
>>  (define (rotate x) (+ A (modulo (- x A -2) 26)))
>>  (regexp-replace* #rx#"[a-z]" str (λ (m) (bytes (rotate (bytes-ref m 0))))))

I used char->integer and integer->char, and I wrote "rotate" to take in the number as well as the character, so I had to deal with positives and negatives, wrapping around more than once, staying within the same case, etc.  And I haven't played with regexps in Racket much, so I used string->list, map, and list->string.

I finally made myself learn a little about regexps for Level 4 (which involved pretty minimal matching, but I thought it was a good excuse).

Stephen Bloch
sbloch at adelphi.edu

Posted on the users mailing list.