[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Something out for which to watch



Peter Dudey notes:

> If you use a function like this:
>
> (defun foo (x) (delete x '(a b c d e f)))
>
> the quoted list '(a b c d e f) is NOT immune to the delete!  Running the
> function could actually change it!

Yes, this a trap, even for experienced Lisp programmers. Don't use quoted
lists in contexts where you also use destructive operations. Note that
using '() for the empty list is always safe.

> I'm not sure if this is a bug in CLISP or not--I was under the impression
> that a quoted, explicit list like that was a constant, and would be
> "re-created" every time the function was run.

No, you always refer to the same constant. CLtL2 p. 105 cites X3J13 vote
<147>: "EVAL and COMPILE are not permitted either to copy or to coalesce
constants appearing in the code they process."

David Loewenstern writes:

> I'm fairly certain that CLtL2 makes this trap specific.

Yes, X3J13 vote <36> (CLtL2 p. 70) states: "It is an error to destructively
modify any object that appears as a constant in executable code, whether
as a self-evaluating form or within a QUOTE special form."

This means that it is the programmer's responsibility. Lisp implementations
can detect such situations by putting such objects into read-only memory
(on operating systems where this is possible, e.g. Unix). But I didn't
implement this because it would disturb beginners when they are playing
around with the interpreter: simple things like (NREVERSE '(1 2 3)) would
signal an error.

                    Bruno Haible
                    haible@ma2s2.mathematik.uni-karlsruhe.de