Changing the list passed as a parameter gives different results in SBCL and CLISP

Can someone explain why I get different results for the next simple program with sbcl and clisp? Is what I'm doing undefined by language, or is one of the two lisp interpreters wrong?

; Modify the car of the passed-in list (defun modify (a) (setf (car a) 123)) ; Create a list and print car before and after calling modify (defun testit () (let ((a '(0))) (print (car a)) (modify a) (print (car a)))) (testit) 

SBCL (version 1.0.51) produces:

 0 0 

CLISP (version 2.49) creates (what I would expect):

 0 123 
+1
common-lisp
source share
1 answer

I agree with the comments of Set and Vsevolod that this behavior is related to your modification of literal data. Try using (list 0) instead of '(0) . Issues related to this are relatively common, so I'll list HyperSpec here.

3.7.1 Changing letter objects :

The consequences are: undefined if literal objects are destructive modified.

definition of "literal" :

literal adj. (of an object) directly referenced in the program than calculated by the program; that is, as data in the form of a quote or, if the object is a self-evaluating object, appears as unquoted data. `` In the form of (cons "one" '("two")), the expressions "one", ("two") and "two" are literal objects. ''

Please note that often (in many implementations), if you change the values ​​of the literal value, you really modify them in the code itself - write the self-editing code. Your sample code will not work as you expect.

Example code in CCL:

 CL-USER> (defun modify (a) (setf (car a) 123)) MODIFY CL-USER> (defun testit () (let ((a '(0))) (print (car a)) (modify a) (print (car a)))) TESTIT CL-USER> (testit) 0 123 123 CL-USER> (testit) 123 123 123 

Take a look at the second testit evaluation, where let itself already contains the modified value, so the first print also gives 123 .

Also see Lisp, cons and (number number) difference , where I explained this in more detail or the question related to Vsevolod's comment above.

+1
source share

All Articles