From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Drew Adams Newsgroups: gmane.emacs.help Subject: side effects, list sharing [was: `append' vs. `nconc'] Date: Thu, 31 Dec 2015 09:56:12 -0800 (PST) Message-ID: References: <568164D8.6050700@ojkastl.de> <87io3iyr7t.fsf@debian.uxu> <87si2kezg1.fsf@mithlond.arda> <87a8osrlj4.fsf_-_@debian.uxu> <87lh8bgafa.fsf@mithlond.arda> <87r3i3ch63.fsf@kuiper.lan.informatimago.com> <87si2jqee2.fsf@debian.uxu> <20151231093045.GB19900@tuxteam.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1451584610 8801 80.91.229.3 (31 Dec 2015 17:56:50 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 31 Dec 2015 17:56:50 +0000 (UTC) To: tomas@tuxteam.de, help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Dec 31 18:56:38 2015 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 1aEhSn-0003xd-6O for geh-help-gnu-emacs@m.gmane.org; Thu, 31 Dec 2015 18:56:37 +0100 Original-Received: from localhost ([::1]:56430 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aEhSj-0002xA-Jf for geh-help-gnu-emacs@m.gmane.org; Thu, 31 Dec 2015 12:56:33 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:43664) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aEhSZ-0002x4-7b for help-gnu-emacs@gnu.org; Thu, 31 Dec 2015 12:56:24 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aEhSW-0004Pt-1N for help-gnu-emacs@gnu.org; Thu, 31 Dec 2015 12:56:23 -0500 Original-Received: from aserp1040.oracle.com ([141.146.126.69]:29732) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aEhSV-0004Ph-Rt for help-gnu-emacs@gnu.org; Thu, 31 Dec 2015 12:56:19 -0500 Original-Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id tBVHuD4V016505 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Thu, 31 Dec 2015 17:56:16 GMT Original-Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserv0022.oracle.com (8.13.8/8.13.8) with ESMTP id tBVHuDFC003529 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Thu, 31 Dec 2015 17:56:13 GMT Original-Received: from abhmp0001.oracle.com (abhmp0001.oracle.com [141.146.116.7]) by aserv0122.oracle.com (8.13.8/8.13.8) with ESMTP id tBVHuDAc031804; Thu, 31 Dec 2015 17:56:13 GMT In-Reply-To: <20151231093045.GB19900@tuxteam.de> X-Priority: 3 X-Mailer: Oracle Beehive Extensions for Outlook 2.0.1.9 (901082) [OL 12.0.6691.5000 (x86)] X-Source-IP: aserv0022.oracle.com [141.146.126.234] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 141.146.126.69 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:108554 Archived-At: Thomas wrote: > nconc is "just an optimization", because GC isn't ideal. > So a good rule of thumb: > - it's very obvious ... > - you are really pressed for time ... > Otherwise... just don't use it. _Just don't use side effects_, at least those such as `nconc' that share list structure, is the right starting place, and is definitely the best rule of thumb. You will _never_ go wrong with it as your motto, in code you write. If, however, you modify code written by others, who do not play by this simple rule, then you had better learn what it is all about. (By "you" I mean each of us.) Lisp is Lisp. It is not Haskell or lambda calculus. The two guidelines that Thomas listed are fine. But the most important use case for side effects using functions such as `nconc' has to do primarily with neither space nor time optimization: it is _sharing of list structure_ for combined effect. IOW, precisely the thing that can get you in trouble is the main reason for intentionally using such functions: you _want_ a change in one list to automatically change another in the same way. You do that by making them share some list structure. And yes, such things are fraught with gotchas, exactly like the sharing of data using global variables. When a bug rears its ugly head, it is just as likely that you see only an ugly pimple on a foot or elbow or tail, and not on the head at all, and it is likely that the noticeable manifestation will occur only long after you made the change to the list. Bonjour les d=C3=A9gats ! Again, this is _exactly_ like the problems that arise from using global variables. They closely _couple_ together different program pieces and their logic. And this is used in Lisp for exactly the same reason that shared data has been used in other languages. IOW, this is nothing new, even if its form might be new to=20 someone who is unused to Lisp lists. If you are coming to Lisp from Haskell then you might have a lot to learn in this regard. If you are coming to it from Fortran then the data-sharing part is not new (but the rest of Lisp is). This is Real Lisp (TM), and it is not for the faint-hearted. And yes, we can all be bitten occasionally by the gotchas, and we _all are_. And it can be difficult to track down the culprit code. Difficult or fun, depending on how much free time you have and how much you enjoy playing with Lisp. You can say that "nconc is 'just an optimization'", but that is true only for some very general, or very complicated and program-specific, meaning of "optimization". And it is not necessarily "because GC isn't ideal". If you never step off into List-Modification Land then it is not important how such so-called "destructive" functions are characterized. But if you do venture there, e.g., to modify some existing code that shares list structure, then know how, where, and why the structure is shared, to start with. And even then, keep your wits about you, and keep a look out for dragons over your shoulder and under your feet.