[racket] string-trim : an implementation & a question

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Sat Apr 2 21:28:24 EDT 2011

One last note for the evening: because scan is a local macro you can
actually leave off the 's' argument, like this:

(define (string-trim.1 s)
  (define-syntax scan
    (syntax-rules ()
      ((_ start end step)
       (for/first ((i (in-range start end step))
                   #:when (not (char-whitespace? (string-ref s i))))
         i))))

  (let* ((len (string-length s))
         (last-index (sub1 len))
         (start (or (scan 0 len 1) 0))
         (end (or (scan last-index (sub1 start) -1) last-index)))
    (substring s start (add1 end))))

Any free variables in the result of a macro get picked up at the
definition site of the macro; that means that things work mostly like
you'd expect, namely that the "s" on the right-hand side of the
syntax-rules picks up the "s" in the parameter list of string-trim.

(You may have known this already; apologies if so.)

Robby


Posted on the users mailing list.