[racket] specifying strings without escaping backslash

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Dec 5 20:24:48 EST 2013

Yesterday, Laurent wrote:
> Or better here: @values{C:\Users\Harry\SANSKRIT\GRETIL
> ALL\adyappu.htm} to avoid the superfluous ~a operation.

Using `values' is not a good idea since it works only if you don't use
nested @s or newlines.  If these things are included, you'll end up
with a form that is equivalent to (values x y z) and things will
probably break in a confusing way.


> I've just surprised myself testing @(...){...} and seeing that it
> works: @(lambda(x)x){C:\Users\Harry\SANSKRIT\GRETIL ALL\adyappu.htm}

That's intentional, and more than that, any form can be used including
@-forms, which means that to write a function that takes in two blocks
of text you can do something like this:

    @(define ((foo . text1) . text2) ...stuff...)

    @@foo{some text}{some more text}

(This is a simplified example again, where you just get a single
argument, but in the general case you'd have multiple inputs for both
texts.)


Yesterday, Harry Spier wrote:
> 
> 2)
> What am I doing wrong here.  -- everything :-)
> I'm trying to change the command character from @ to #  so that I
> can have raw strings containing both \ and @  looking at the racket
> documentation here:
> http://docs.racket-lang.org/scribble/reader-internals.html?q=at-exp#
> %28def._%28%28lib._scribble%2Freader..rkt%29._make-at-readtable%29%29
> [...]

It's possible to do that, though you'll need to implement your own
reader.  Not too hard, since the scribble/reader functionality can
build the appropriate table, but two notes before you go that route:

1. Overriding "#" is a bad idea, since it has special meaning in
   Racket code.  Some obvious examples that you'll lose are boolean
   literals, vector literals, and the convention that "#<" is an
   unreadable value -- a convention that can lead to copy/paste
   surprises if broken.

2. If you want to include more free-form text, there's a reader
   feature that allows you to use customized tokens:

     @foo|{ some text including @s }|

   to use nested forms, you'd need to use "|@".  If that's not enough,
   then you can also include more things between the braces and the
   bars:

     @foo|==!!=={ even more free text, including @|s etc }==!!|


Yesterday, Greg Hendershott wrote:
> I have @~a{} in muscle memory because it's handy for a variety of
> purposes, including as an alternative to format/printf.
> 
>     #lang at-exp racket
> 
>     (define x 1)
>     (define y 10)
> 
>     (format "x is ~a, y is ~a, and x + y is ~a" x y (+ x y))
>     ;; or
>     @~a{x is @x, y is @y, and x + y is @(+ x y)}
> 
>     ;; both => "x is 1, y is 10, and x + y is 11"

Allowing such things was very intentional in the design...  The
`scribble/text' language goes further in that it allows many more
things in the contents for printing.  There's no to-string form for it
though, since I wasn't sure that it's a good idea -- mostly because
people would be tempted to do something like

    (display @to-string{stuff})

where a long string gets allocated for no good reason.  Using `output'
instead usually looks like this:

    (output @list{
      anything you want
    })

since lists are just scanned recursively by `output'.  As a side note,
this use of lists makes some things very convenient -- instead of
tricky code that uses `append-map' or `format's, `string-append's and
`~a's, you just use lists, nesting them as much as you want.


> p.s. I believe http://www.mail-archive.com/users@racket-lang.org/msg07162.html
> predates `~a` being added to Racket.

(It does.)

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


Posted on the users mailing list.