r/Common_Lisp • • Feb 27 '23

String is immutable

Error given : Destructive function SB-KERNEL:%SETELT called on constant data: "MYSTRING"

(load "~/quicklisp/setup.lisp")
(declaim (optimize (speed 3) (safety 3)))
(defun main ()
    (let ((s "MYSTRING"))
        (setf (elt s 0) #\H)
        (print s)))
0 Upvotes

5 comments sorted by

12

u/agrostis Feb 27 '23

See CLHS 3.7.1: Modification of Literal Objects.

You won't get this error if you construct a non-literal string, e. g., as (copy-seq "MYSTRING").

4

u/agrostis Feb 27 '23

Btw., reddit doesn't grok triple backticks. Its syntax for code blocks is to prefix every line with four spaces.

3

u/Ytrog Feb 27 '23

Old reddit doesn't; new reddit does.

4

u/lispm Feb 27 '23

make sure you post your code correctly formatted and indented: https://www.reddit.com/r/Common_Lisp/comments/a4przy/how_to_post_formatted_lisp_code_with_the_new/

Make sure that your Lisp code is readable for other human Lisp readers.