[racket] Some design "whys" of regexps in Racket

From: Eli Barzilay (eli at barzilay.org)
Date: Sat Jun 4 17:03:11 EDT 2011

5 hours ago, Rodolfo Carvalho wrote:
> Good to learn about here strings. I've seen them somewhere but never
> paid much attention.
> For short strings I'd rather type things like "\\\\" than
> #<<END
> \\
> Because the latter is more verbose and breaks the visual flow.
> Both `#<<END' and `END' must come in their own lines, and no line
> breaks are considered immediately after and before these tokens,
> respectively (reasonable).

Yes, that's the problem with them, in any language.  I've mentioned
the scribble syntax (which is probably easy to miss since "scribble"
gets more strongly associated with "documentation") as a better way to
do this.  Here's an example:

  #lang at-exp racket/base
  (require racket/string)
  (define rx (λ xs (regexp (string-append* xs))))
  (define px (λ xs (pregexp (string-append* xs))))

  ;; and now you can use:
  (define a-digit @px{\d})
  (define digit+newline+digit

note that the last example is properly indented, but the regexp
doesn't have the extra spaces in it.

(And BTW, this syntax can do much more than just a convenient
replacement for here-strings -- you can also get interpolation to the
point of building your own dynamic DSL.)

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.