Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Scheme Programming Language

newbie - STRING-APPEND (did I bereak it)


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.

Don't worry. The string doesn't actually contain the backslash, its just
being displayed in the scheme REPL. If printing the string to a file, or
just displaying it ("(display (string #\"))"), the backslash isn't there.

Mike <mcu87@bigpond.net.au> wrote:
> (string-append "hello" (list->string (list #\" #\' #\.))) ;==> "hello\"'."

Looks fine. What you get here is called the external representation
of the resulting string. The external representation of every object
is unambiguous and therefore, it has to escape double quotes inside
of strings. The backslash is not really a character in the string,
it is part of the representation:

(string #\H #\i) => "Hi"              ; Content of the string: Hi
(string #\" #\H #\i #\") => "\"Hi\""  ; Content of the string: "Hi"

"\"\"" this is one expression (a string) containing two quotes.
""""   these are two expressions, both empty strings.

Scheme uses READ to turn external representations into expressions,
and WRITE to convert expressions to external representation:

(write (read)) "Hi"      writes  "Hi"
(write (read)) "\"Hi\""  writes  "\"Hi\""

Scheme environments use WRITE to display normal forms, which is
why your quote character is prefixed by a backslash. If you want
to see the content of the string instead, use DISPLAY:

(display (read)) "Hi"      writes  Hi
(display (read)) "\"Hi\""  writes  "Hi"
(display
  (string-append "hello" (string #\" #\' #\.)))  writes  hello"'.

> In case you can't see it due to my font size, [...]

This is Usenet, so if I could not read it, /my/ font size was wrong.

> (a) Is there a way of turning a single character, of form #\a,  into a
> string [...]

Yes: (string #\x) => "x"
     (string #\x #\y #\z) => "xyz"
     (string) => ""

but it will probably use LIST->STRING eventually:

(define (string . x) (list->string x))

> (b) While (list->string (list #\")) ;==> "\"" I cannot string-append this
> result to another string without the silly backslash appearing.   So:

Exercise: create a string that /really/ contains a backslash
and see what it looks like.

--
Nils M Holm <n m h @ t 3 x . o r g> -- http://t3x.org/nmh/

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc