Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!samsung!think!think.com!barmar From: barmar@think.com Newsgroups: comp.lang.lisp Subject: Re: NCONC & Functions Message-ID: <31792@news.Think.COM> Date: 29 Nov 89 03:56:39 GMT References: <464@ntcsd1.UUCP> Sender: news@Think.COM Distribution: na Organization: Thinking Machines Corporation, Cambridge MA, USA Lines: 48 In article <464@ntcsd1.UUCP> mps@ntcsd1.UUCP (Michael Smith) writes: > >(DEFUN foo1 () '(a b c)) > >FOO1 > >(foo1) > >(A B C) > >(NCONC (foo1) '(d e f)) > >(A B C D E F) > >(foo1) > >(A B C D E F) Someone else already explained what is going on -- your NCONC is modifying the actual list that is in your function. Remember, QUOTE returns its argument, *not* a copy of it. >(DEFUN foo2 () (LIST 'a 'b 'c)) is unaffected in the same situation. Since this creates a new list every time it is called, destructive operations cannot affect it. >I have been using the rule: don't use destructive operations on data struc- >tures you care about. Now this turns out to be insufficient. A better rule is: don't return constant data structures from functions, unless the function is documented to return a constant (so the caller knows not to try to modify it). A compiler would be justified in storing constant data structures on read-only pages, so the above NCONC *could* result in a core dump! > Is there a >rule regarding when destructive operations can affect the values returned >by functions they are not part of? If you modify a constant that came from a function, you may be modifying the function. The wording of the rule in the ANSI CL working draft is "the consequences of using a destructive operation on a constant are undefined." > I'm told that even FOO2 is affected when >running Franz on a DEC station. That's a bug in Franz. Sounds like an incorrect compiler optimization to me -- it's translating a call to LIST all of whose arguments are quoted constants into a single quoted constant. Since LIST is supposed to cons a new list each time it is called, this changes the semantics drastically. Barry Margolin, Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar