[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Different behaviors of evaluator and compiler
Date: Wed, 21 Aug 1991 08:25 EDT
From: CLanning@pdesds1.atg.trc.scra.org (Craig Lanning)
Date: Wed, 21 Aug 91 12:25+0100
From: Vincent Keunen <email@example.com>
This is a simple function to demonstrate the strange behaviour:
(defun test ()
(let ((result '(nil)))
(loop for item in '(a b c)
do (nconc result (list item)))
(test) ;when the function definition is evaluated returns
(NIL A B C) ;...which is what I want
Try calling it again, and you'll see that it doesn't always do what you
want. The second time it will return (NIL A B C A B C), the next time
it will return (NIL A B C A B C A B C), etc.
(test) ;when the function definition is compiled returns
Error: Attempt to RPLACD a list that is embedded in a structure and
therefore cannot be RPLACD'ed. The list is (NIL)
While in the function SYS:RPLACD-ESCAPE SYS:*NCONC TEST
Can someone help me on this one?
The error reported after the function is compiled has to do with the way
constants are stored in the COMPILED-FUNCTION object. No doubt, someone
else will send you a more in-depth explanation of why that happens.
Someone else just asked this same question less than two weeks ago.
Yes, the problem is that constants are stored in the function object,
and lists in structure regions can't be RPLACD'ed.
In general, Common Lisp doesn't permit constants to be modified. See
p.70 of CLtL 2nd Edition, the second paragraph of section "Self
Evaluating Forms". This restriction exists to permit implementations to
share structure between multiple instances of equivalent constants
(referred to as "coalescing constants"), to store constants in read-only
memory, and to obviate copying constants when evaluating or compiling