all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: tomas@tuxteam.de, help-gnu-emacs@gnu.org
Subject: side effects, list sharing   [was: `append' vs. `nconc']
Date: Thu, 31 Dec 2015 09:56:12 -0800 (PST)	[thread overview]
Message-ID: <d33aa2e4-3f3a-44f9-8e24-61bbe3af6313@default> (raw)
In-Reply-To: <20151231093045.GB19900@tuxteam.de>

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égats !

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 
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.



  reply	other threads:[~2015-12-31 17:56 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-28 16:35 Error: Setting the shell in sh-mode via a local variable does not work Johannes Kastl
2015-12-29  1:32 ` Emanuel Berg
2015-12-29 18:33   ` Johannes Kastl
2015-12-30 14:53     ` Emanuel Berg
2015-12-30 15:15       ` Robert Thorpe
2015-12-30 15:30         ` Emanuel Berg
2015-12-30 17:42         ` Johannes Kastl
2015-12-30 15:19   ` Teemu Likonen
2015-12-30 15:41     ` `append' vs. `nconc' (was: Re: Error: Setting the shell in sh-mode via a local variable does not work) Emanuel Berg
2015-12-30 16:37       ` `append' vs. `nconc' Teemu Likonen
2015-12-31  3:37         ` Emanuel Berg
     [not found]         ` <mailman.1400.1451533083.843.help-gnu-emacs@gnu.org>
2015-12-31  3:45           ` Pascal J. Bourguignon
2015-12-31  4:00             ` Emanuel Berg
2015-12-31  9:22               ` tomas
2015-12-31 18:48                 ` Emanuel Berg
     [not found]             ` <mailman.1402.1451534421.843.help-gnu-emacs@gnu.org>
2015-12-31  5:36               ` Pascal J. Bourguignon
2015-12-31  3:50         ` Emanuel Berg
     [not found]         ` <mailman.1401.1451533833.843.help-gnu-emacs@gnu.org>
2015-12-31  5:37           ` Pascal J. Bourguignon
2015-12-31  7:13             ` Emanuel Berg
2015-12-31  9:30               ` tomas
2015-12-31 17:56                 ` Drew Adams [this message]
2015-12-31 19:00                   ` side effects, list sharing [was: `append' vs. `nconc'] Emanuel Berg
2015-12-31 19:15                     ` Drew Adams
2016-01-01 13:20                       ` Emanuel Berg
2015-12-31 18:51                 ` `append' vs. `nconc' Emanuel Berg
     [not found]                 ` <mailman.1440.1451588113.843.help-gnu-emacs@gnu.org>
2016-01-01  2:53                   ` Barry Margolin
2016-01-01 13:26                     ` Emanuel Berg
     [not found]             ` <mailman.1406.1451546036.843.help-gnu-emacs@gnu.org>
2015-12-31  8:54               ` Pascal J. Bourguignon
2015-12-31  7:31           ` Teemu Likonen
2015-12-31 18:35             ` Emanuel Berg
2015-12-31 20:04               ` Teemu Likonen
2016-01-01 13:23                 ` Emanuel Berg
2016-01-01 14:02                   ` Teemu Likonen
2016-01-01 18:31                     ` Emanuel Berg
     [not found]             ` <mailman.1438.1451586967.843.help-gnu-emacs@gnu.org>
2016-01-01  6:25               ` Pascal J. Bourguignon
2016-01-01 13:31                 ` Emanuel Berg
2016-01-01 15:04                   ` Drew Adams
     [not found]     ` <mailman.1353.1451490125.843.help-gnu-emacs@gnu.org>
2015-12-30 16:18       ` Pascal J. Bourguignon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d33aa2e4-3f3a-44f9-8e24-61bbe3af6313@default \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=tomas@tuxteam.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.