[plt-scheme] How to get the reader to accept a single backslash?

From: Robby Findler (robby at cs.uchicago.edu)
Date: Sun Sep 2 21:00:31 EDT 2007

You're confusing the contents of the string and how the string prints
out in the REPL. Note that printing strings with `write' (and things
that eventually call write, like the REPL) print in such a way that
they can be read back in as program text (for strings, anyways) and
still be the same string. The function `display', on the other hand,
prints things in a way that makes them look "right" for people to read

So, with that in mind, I think the string you want is "C\\#". That
string has three characters:

  > (string-length "C\\#")

the first one is C, the second is a backslash, and the third is a number sign.

In some sense, this all stems from the problem of making a string that
has a quote character in it. In order to avoid confusing the character
that indicates the string is finished form the quote character that is
in the string, we have do something. So, in mz we stick a slash in
front of the quote, like this:

  > (string-length "\"")

Of course, that means that the backslash character has to be special
now, too. So, for that one, we just use two of them, as above.

Also, try this:

  (begin (for-each display (string->list "C\\#")) (newline))

Hope that helps.


On 9/2/07, Grant Rettke <grettke at acm.org> wrote:
> Hi,
> I'm trying to generate the string "C\#" but can't seem to accomplish this.
> Here is what I've tried:
> "C\#" -> won't execute in the repl
> ;
> (string-append "C" "\" "#") -> won't execute in the repl
> ;
> (string-append "C" "\\" "#") -> "C\\#"
> ;
> (string-append "C" (symbol->string '\) "#") -> won't execute in the repl
> ;
> (string-append "C" (symbol->string '\\) "#") -> "C\\#"
> The problem is that I don't see how to input a single backslash since
> it is the escape sequence indicator. I don't understand why \\ shows
> up as double slash in strings.
> What am I doing wrong? Where should I read up on this?
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.