If strings are vectors, why are they immutable?

if strings are character vectors, and vector elements can be accessed with elt, and elt is setf-able - then why are strings immutable?

+4
source share
2 answers

Strings are not immutable in Common Lisp, they are mutable :

* is the last result of listening to Lisp

CL-USER 5 > (make-string 10 :initial-element #\-)
"----------"

CL-USER 6 > (setf (aref * 5) #\|)
#\|

CL-USER 7 > **
"-----|----"

CL-USER 8 > (concatenate 'string "aa" '(#\b #\b))
"aabb"

CL-USER 9 > (setf (aref * 2) #\|)
#\|

CL-USER 10 > **
"aa|b"

The only thing you do not need to do is change the literal lines in your code. The consequences are undefined. This is the same problem with other literal data.

For example, a file contains:

(defparameter *lisp-prompt* "> ")

(defparameter *logo-prompt* "> ")

compile-file, , . . .

.

.

+14

, . :

* (defparameter *s* (format nil "aaa"))
*S*

* (setf (elt *s* 1) #\b)
#\b

* *s*
"aba"
0

All Articles