From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: Why doesn't nconc change my variable? Date: Sun, 05 Oct 2014 20:12:26 +0200 Organization: Informatimago Message-ID: <87fvf2mlmd.fsf@kuiper.lan.informatimago.com> References: <87y4svl2ku.fsf@wmi.amu.edu.pl> <3867d7c2-936d-4441-91d6-3b12dc959391@default> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1412533229 8741 80.91.229.3 (5 Oct 2014 18:20:29 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 5 Oct 2014 18:20:29 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Oct 05 20:20:21 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1XaqPt-0002Cz-5X for geh-help-gnu-emacs@m.gmane.org; Sun, 05 Oct 2014 20:20:21 +0200 Original-Received: from localhost ([::1]:48300 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XaqPs-0003IU-NH for geh-help-gnu-emacs@m.gmane.org; Sun, 05 Oct 2014 14:20:20 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 101 Original-X-Trace: individual.net sOaKkt3BYMQ3LaFSagh2MgYwSbv5qokd3pfe5b0xmRGM+EWCil Cancel-Lock: sha1:MWRhMmU3OWZlMmI1NDYxMDFkMDBkOTEwMDI5OGU2MjQ2NGM0NzRiOA== sha1:jTslTt4CusRG1Kgbfo+2RYaQpPU= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:208015 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:100291 Archived-At: Marcin Borkowski writes: > On 2014-10-05, at 03:58, Drew Adams wrote: > >>> (setq my-list ()) >>> (nconc my-list '("wtf")) >>> >>> and my-list is still nil. If, OTOH, I do >>> (setq my-list ()) >>> (setq my-list (nconc my-list '("wtf"))) >>> >>> my-list is ("wtf"). >>> >>> Why is that so? I though nconc is supposed to change all its >>> arguments but the last one. Is the latter construct a correct >>> way of adding an element at the end of the list? >> >> No, it's not supposed to do that. You discovered just what it >> does do. And you could have discovered it earlier by reading >> some doc. See the Elisp manual, node `Rearrangement' (found by >> doing `i nconc' in the manual). > > I did read it. And a few times, for that matter. > > You see, one of the worst intellectual mistakes you can make is to have > a wrong mental model of something. This seems to be the case here, and > that's why I really want to grok this issue. > >> See also node `Sets and Lists', in particular this (about `delq', >> but the same idea applies to other destructive list operations): >> >> Note that `(delq 'c sample-list)' modifies `sample-list' to splice >> out the third element, but `(delq 'a sample-list)' does not splice >> anything--it just returns a shorter list. Don't assume that a variable >> which formerly held the argument LIST now has fewer elements, or that >> it still holds the original list! Instead, save the result of `delq' >> and use that. Most often we store the result back into the variable >> that held the original list: >> >> (setq flowers (delq 'rose flowers)) > > That I didn't read earlier; I've read it now, and still don't get it. > Sorry. (Edit: while I was writing the rest of this post, things > apparently got more clear, but I'll leave the rest, since if I'm right, > this might be (hopefully) instructive for others reading it, and I might > even want to save it to post it on my blog.) > > Now I have two choices. One is to install the C source and try to read > it. This I cannot do now (time constraints, and while I did learn some > C, it was, what, fifteen years ago or so). That's why I'm asking here. > Please bear with me. This should also be good for Emacs, since it may > be the case that if I cannot understand the manual, someone else might > have troubles with that, too, so maybe the manual is buggy. > > What I thought was essentially this: under the hood, a list is a pointer > (using C terminology) to a cons cell. The car of the cons cell is a > pointer to some data (say, a string), and the cdr - a pointer to the > next cons cell. > > What I thought is that if you say, e.g., > > (setq my-list ("hello" "world")) > > and then > > (nconc my-list '("wtf")) > > then my-list points to the list ("hello" "world" "wtf"). This seems to > be indeed the case. (And the wtf's get added if I repeat the nconc step > - I confirmed that experimentally.) You did not. Your experience was wrong or incomplete. (setf print-circle t) (defun strange (list) (nconc list '("wtf"))) (strange (strange (list 1 2 3))) --> (1 2 3 . #1=("wtf" . #1#)) (setf print-circle nil print-length 15 eval-expression-print-length 15) (defun strange (list) (nconc list '("wtf"))) ; you need to redefine it. (strange (strange (list 1 2 3))) --> (1 2 3 "wtf" "wtf" "wtf" . #3) Actually, if you try to call strange^3, it falls into an infinite loop, because after strange^2, "wtf" is in a circular list. Again, do not mix immutable data with mutable data, if you want to mutate the result later! Use: (nconc list (list "wtf")), not quote. -- __Pascal Bourguignon__ http://www.informatimago.com/ “The factory of the future will have only two employees, a man and a dog. The man will be there to feed the dog. The dog will be there to keep the man from touching the equipment.” -- Carl Bass CEO Autodesk