From: michael r murphy
Subject: strings with escape characters
Date: 
Message-ID: <27E0EDEE.49E5@ibma0.cs.uiuc.edu>
I'm rather new to using Lisp and I am having a problem with strings.
I would like to read in a C string and keep the escape characters 
as is.  I have enclosed a sample of my problem.  What I would like
is:

<cl> (setf foo "\nThis is a c string\n")

"\nThis is a c string\n"

I just want to get the \ in there without it being interpreted as 
an escape.  I can then handle printing it out.  Help.

Script started on Fri Mar 15 09:46:15 1991
murphy|ibma0|[51]% acl
Allegro CL 3.2.beta.1 [IBM RS/6000] (0/0/0)
Copyright (C) 1985-1990, Franz Inc., Berkeley, CA, USA
<cl> (setf foo "\nThis is a c string\n")

"nThis is a c stringn" 
<cl> (setf foo |"\nThis is a c string\n"|)
Error: Attempt to take the value of the unbound symbol |"nThis is a c stringn"|
[1] <cl> :exit
; Exiting Lisp
murphy|ibma0|[52]% ^D
script done on Fri Mar 15 09:47:00 1991

······@cs.uiuc.edu  OR  ····@uiuc.edu
From: Barry Margolin
Subject: Re: strings with escape characters
Date: 
Message-ID: <1991Mar15.184822.17411@Think.COM>
In article <·············@ibma0.cs.uiuc.edu> ······@ibma0.cs.uiuc.edu (michael r murphy) writes:
>I would like to read in a C string and keep the escape characters 
>as is.  I have enclosed a sample of my problem.  What I would like
>is:
>
><cl> (setf foo "\nThis is a c string\n")
>
>"\nThis is a c string\n"
>
>I just want to get the \ in there without it being interpreted as 
>an escape.  I can then handle printing it out.  Help.

You have to double the slash, i.e.

(setf foo "\\nThis is a c string\\n")

><cl> (setf foo |"\nThis is a c string\n"|)
>Error: Attempt to take the value of the unbound symbol |"nThis is a c stringn"|

|...| is a symbol, not a string, so you have to use single-quote to prevent
it from being evaluated.  Also, backslash is interpreted inside vertical
bars (to allow you to include a vertical bar as a character in a symbol
name), so you still have to double it:

(setf foo '|"\\nThis is a c string\\n"|)

Note that in both cases there is only one backslash in the actual string.
The doubling is only necessary during I/O.  Notice:

> (setq *string* "\\n")
"\\n"
> (length *string*)
2
> (coerce *string* 'list)
(#\\ #\n)
> (write-string *string*)
\n
"\\n"
--
Barry Margolin, Thinking Machines Corp.

······@think.com
{uunet,harvard}!think!barmar