Hi all,
I'm still playing with strings and lists (dangerous to let me in the
playground :) and seem to have come up against a unexpected result using
string-append when used in conjunction with list->string where list is of
valid character types designated by #\a etc.
I looked in the on-line help - no joy.
(string-append "hello" (list->string (list #\" #\' #\.))) ;==> "hello\"'."
In case you can't see it due to my font size, the list contains characters
double quote, apostrophe and a period. The apostrophe and period appear as
I expected in the result string. However, the double quote is preceded by a
back slash character. And according to the on-line help (below), the
backslash is Schemes way of allowing me to use such characters as a double
quote - just might not look the way I want.
I guess I could treat this as a special case and trap for the existence of a
double quote character but then I don't know if other character types might
also give the same result. And I still have no idea how to avoid the
backslash when converting the double quote case.
(define char->string
[lambda (char)
(cond
[(equal? #\" char)
<...something clever...> ] ; I'm not that clever :)
[(or (char-alphabetic? char)
(char-numeric? char)
(char-symbolic? char)
(char-punctuation? char)
(char-whitespace? char))
(list->string (list char))]
[else
#f])])
(char->string #\.); ==> "."
(char->string #\' ; ==> "'"
(char->string #\a); ==> "a"
I added the else condition out of habit and to force whatever called
char->string to break so I could see what character type I had not accounted
for.
(a) Is there a way of turning a single character, of form #\a, into a
string aside from (list->string (list (car (list #\a #\b)))) because that
just seems silly. I haven't found an equivalent to number->string that I
can use for characters.
(b) While (list->string (list #\")) ;==> "\"" I cannot string-append this
result to another string without the silly backslash appearing. So:
(string-append
(list->string (list #\")) "a"
(list->string (list #\"))) ;==> "\"a\"". (see on-line help for
explanation you say)
Is there a work around? Or should I just accept that this is what happens
with double quotes in Scheme because strings are denoted by the use of the
double quote. As in: ""a"" both looks silly and can't exist. Scheme
interprets the closed double quotes to be a null string and the a as an
object / symbol.
Kindest regards,
Mike
From DrScheme help:
Symbols containing special characters (including delimiters) are expressed
using an escaping backslash (``\'') or quoting vertical bars (``|''):
* A backslash preceding any character includes that character in the symbol
literally; double backslashes produce a single backslash in the symbol.
* Characters between a pair of vertical bars are included in the symbol
literally. Quoting bars can be used for any part of a symbol, or the whole
symbol can be quoted. Backslashes and quoting bars can be mixed within a
symbol, but a backslash is not a special character within a pair of quoting
bars.