Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

String / character array

34 views
Skip to first unread message

yoxoman

unread,
Feb 5, 2012, 5:32:07 PM2/5/12
to
Hello,

No doubt this is a trivial question, but I can't find any satisfying
answer.

Is there a difference between a string and an array of character ?

Because the following expression:

(stringp #(#\h #\e #\l #\l #\o))

evaluates to nil...


Thanks.

Pascal J. Bourguignon

unread,
Feb 5, 2012, 8:21:56 PM2/5/12
to
yoxoman <inv...@invalid.invalid> writes:

> Is there a difference between a string and an array of character ?

Yes and no.


> Because the following expression:
>
> (stringp #(#\h #\e #\l #\l #\o))
>
> evaluates to nil...

Sure. That's because #( creates a vector of T, instead of a vector of
CHARACTER or of BASE-CHAR.

A STRING is a VECTOR of CHARACTER or a VECTOR of BASE-CHAR (the later is
a BASE-STRING).

[3]> (coerce #(#\h #\e #\l #\l #\o) '(vector character))
"hello"

So everything works as if we had those definitions:

(deftype base-string (&optional (size '*)) `(vector base-char ,size))
(deftype string (&optional (size '*)) `(vector character ,size))

But furthermore, STRING is a CLOS class. So you can write methods
specialized on STRING, but not on BASE-STRING.


Otherwise, ARRAYs may have more than one dimension, and
multidimensionnal arrays even of characters, are not strings. They can
however be happily used, eg. to draw ASCII ART pictures.

See for example:
http://gitorious.org/com-informatimago/com-informatimago/blobs/master/common-lisp/picture/picture.lisp#line122





A little funny factoid: a vector of nothing of size 0 is also a string,
and may be printed as "".

[1]> (stringp (make-array 0 :element-type nil))
T
[2]> (make-array 0 :element-type nil)
""
[4]> (string= (make-array 0 :element-type nil) "")
T
;; and even:
[7]> (equal (make-array 0 :element-type nil) "")
T
;; but:
[6]> (mapcar 'array-element-type (list (make-array 0 :element-type nil) ""))
(NIL CHARACTER)
;; :-)

--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

Antti J Ylikoski

unread,
Feb 6, 2012, 4:32:34 AM2/6/12
to
Interestingly:

------------------------------------------

CL-USER> (typep #(#\h #\e #\l #\l #\o) 'string)
NIL
CL-USER> (typep #(#\h #\e #\l #\l #\o) 'array)
T
CL-USER>

------------------------------------------

with the GNU EMACS, the SLIME and the CLISP (which is not at all a bad
LISP implementation!)

Which resonates well with that which P.J.B, the group's resident LISP
expert, said. The expression #( ...... ) creates an array of objects of
the type T, the supertype of all types.

yours, andy


Tamas Papp

unread,
Feb 6, 2012, 7:35:36 AM2/6/12
to
On Mon, 06 Feb 2012 02:21:56 +0100, Pascal J. Bourguignon wrote:

> A little funny factoid: a vector of nothing of size 0 is also a string,
> and may be printed as "".
>
> [1]> (stringp (make-array 0 :element-type nil)) T

Isn't that simply because (subtypep nil 'character)?

Tamas

Alex Mizrahi

unread,
Feb 6, 2012, 8:26:48 AM2/6/12
to
> No doubt this is a trivial question, but I can't find any satisfying
> answer.

Um, did you even try?

CL-USER> (type-of #(#\h #\e #\l #\l #\o))

(SIMPLE-VECTOR 5)
CL-USER> (type-of "hello")

(SIMPLE-ARRAY CHARACTER (5))

Or in documentation:

http://www.lispworks.com/documentation/lw50/CLHS/Body/t_string.htm

A string is a specialized vector whose elements are of type character or
a subtype of type character. When used as a type specifier for object
creation, string means (vector character).

Pascal J. Bourguignon

unread,
Feb 6, 2012, 12:32:18 PM2/6/12
to
Basically, yes.
0 new messages