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:04:49 +0200 Organization: Informatimago Message-ID: <87k34emlz2.fsf@kuiper.lan.informatimago.com> References: 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 1412532624 1934 80.91.229.3 (5 Oct 2014 18:10:24 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 5 Oct 2014 18:10:24 +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:10:19 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 1XaqGB-0006yi-Dt for geh-help-gnu-emacs@m.gmane.org; Sun, 05 Oct 2014 20:10:19 +0200 Original-Received: from localhost ([::1]:48249 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XaqGA-0001JD-PD for geh-help-gnu-emacs@m.gmane.org; Sun, 05 Oct 2014 14:10:18 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 85 Original-X-Trace: individual.net 3LVc/CG/4Gt3Xjfko2XuowltL8FNUhq6ECJw6cDWrneR7DWHSv Cancel-Lock: sha1:ODcyMWMyNWY1MjYzMDVlYzQ0NGFmNzZhNjMwMTBhYjc4Y2NiYjlkYQ== sha1:TCrkFdLOGw6iFKN1IP7Snbis3+E= 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:208013 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:100289 Archived-At: Marcin Borkowski writes: > Hi list, > > I don't get it. > > (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"))) Also, you are doing something dangerous here: You are mixing mutable and immutable data. This is dangerous, because now you may believe that your variable contains mutable data, and try to mutate it, and you will actually try to mutate immutable data, (which has usually strange or catastrophic results). (defvar my-list) (setq my-list (list 1 2 3)) ;; now, my-list contains mutable data. You can modify the car or ;; the cdr of any cell in the list. (setq my-list (nconc my-list '("wtf"))) ;; Now, my-list is bound to the list: (1 2 3 "wtf") ;; but this list is made of 3 mutable cons cells, and one immutable ;; cons cell, with one immutable string! If you were to perform any of: (setq my-list (nconc my-list '("bad"))) (setq my-list (car (last my-list) "bad")) (setq (aref (car (last my-list)) 0) ?W) (setq my-list (delete* "wtf" my-list :test (function string=))) ;; etc. you would get strange/catastrophical results: For example, try: (defun strange (list) (nconc list '("wtf"))) (let ((a (strange (list 1 2 3)))) (setf (car (last a)) "BAD") (list a (strange (list 4 5 6)))) --> ((1 2 3 "BAD") (4 5 6 "BAD")) ; WTF, where is "wtf"? If you try with: (setf print-circle t) (let ((a (strange (list 1 2 3)))) (setf (car (last a)) "strange") (list a (strange (list 4 5 6)))) --> ((1 2 3 . #1=("strange")) (4 5 6 . #1#)) this may give you a hint of what's happening. So avoid mixing mutable and immutable data, or consider the result to be immutable (and therefore avoid using destructive operations on it). Notice that append, while it copies the N-1 first arguments, it keeps the last argument, so (append list '("wtf")) is no better than (nconc list '("wtf")). You need (nconc list (list 'wtf)) ; if you want immutable elements or (nconc list (list (format "%s" "wtf"))) ; if you want ; mutable elements. -- __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