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 {}.