[racket] String manipulation without regex opinion

From: Danny Yoo (dyoo at hashcollision.org)
Date: Tue Nov 6 15:29:45 EST 2012

Racket's loop iteration system is extensible; we can extend Racket to
produce a 'for' loop that can accumulate strings.  For example:

;; An example of using for/fold/derived.
;; for/string: a loop variant that produces a string:
(define-syntax (for/string stx)
  (syntax-case stx ()
    [(_ clauses . body)
     (with-syntax ([original stx])
           (for/fold/derived original
                             ([chars/rev '()])
                             (cons (let () . body)

Hopefully we don't have to write this all out from scratch every time we
use it; we should probably sequester this into a library and not need to
look at it again.  :)

But once we shuffle this in a library somewhere, we can use it like any of
the other loop variations, and it makes your subsequent definitions nice
and short:

;; string -> string
;; Removes vowels from the input
(define (remove-vowels word)
  (for/string ([ch (in-string word)]
               #:when (not (member ch (list #\a #\e #\i #\o #\u))))

;; string -> string
;; Replaces spaces in the input with dashes
(define (replace-spaces word)
  (for/string ([c (in-string word)])
    (if (char=? c #\space) #\- c)))

Best of wishes!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121106/12859855/attachment-0001.html>

Posted on the users mailing list.