unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Robin Tarsiger <rtt@dasyatidae.com>
To: Arthur Miller <arthur.miller@live.com>
Cc: emacs-devel@gnu.org
Subject: Re: Ad-hoc list structure tutorializing (was: Lispref add-to-list - doc is unnecessary convoluted)
Date: Fri, 4 Dec 2020 10:14:45 -0600	[thread overview]
Message-ID: <8ac1cf69-73ef-d8b9-df79-5a895f194936@dasyatidae.com> (raw)
In-Reply-To: <b1252cde-bf09-c52a-3fbd-511f3b467a85@dasyatidae.com>

Robin Tarsiger wrote:
> Arthur Miller wrote:
>> Maybe I don't understand lisp enough, but why is this not desirable:
>>
>> (add-to-list '(1 2 3) '4) or (add-to-list (list 1 2 3) 4)
>
> [...] For general list manipulation, the docstring
> explicitly calls out "please do not abuse it in Elisp code, where you
> are usually better off using ‘push’ or ‘cl-pushnew’".

To be abundantly clear on this, by the way, the non-mutating version
of that (without APPEND) is just the cons primitive itself:

  (cons 4 '(1 2 3))
  => (4 1 2 3)

and when APPEND is set, it's almost equivalent to, well, append:

  (append '(1 2 3) (list 4))
  => (1 2 3 4)

What those don't do is store the new list back where the old one
came from, which is where push and cl-pushnew come in for
local-variable/general-programming purposes and where add-to-list
comes in for config-variable-like purposes. The names are something
of a mishmash, admittedly, I imagine for historical reasons.

Note that you can try to "modify" the tail of a list with nconc:

  (let ((x '(1 2 3)))
    (nconc x (list 4)) ;; wrong! see below
    x)
  => (1 2 3 4)

... but you still need the store-back in general, because
the empty list has no cons cells to mutate!

  (let ((x '()))
    (nconc x (list 4)) ;; oops
    x)
  => nil

so that's only usable as an "optimization" in narrow cases where
you need fast append and can arrange the right guarantees about
the actual list structure, including that no other code is going
to be holding onto any of the sublists at the same time (else
they get a nasty surprise). Don't do that without a really good
reason.

(Aside: why not replace add-to-list with cl-pushnew always? Aside
from historical reasons and things like the buffer-local variable
idiom, being able to toggle APPEND is useful when list entries are
going to be processed in a specific order.)

-RTT



  reply	other threads:[~2020-12-04 16:14 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-04  2:17 Lispref add-to-list - doc is unnecessary convoluted Arthur Miller
2020-12-04  2:27 ` Christopher Dimech
2020-12-04  2:36 ` Robin Tarsiger
2020-12-04  3:29   ` Arthur Miller
2020-12-04  3:35   ` Arthur Miller
2020-12-04  8:35     ` Lars Ingebrigtsen
2020-12-04  8:29 ` Eli Zaretskii
2020-12-04 15:28   ` Arthur Miller
2020-12-04 15:56     ` Robin Tarsiger
2020-12-04 16:14       ` Robin Tarsiger [this message]
2020-12-04 20:24         ` Ad-hoc list structure tutorializing Stefan Monnier
2020-12-04 21:12           ` Christopher Dimech
2020-12-05  7:45             ` Eli Zaretskii
2020-12-06  5:45               ` Richard Stallman
2020-12-06  5:57                 ` Robin Tarsiger
2020-12-06 14:01                   ` Arthur Miller
2020-12-06  5:39             ` Richard Stallman
2020-12-04 21:40           ` add-to-list vs cl-pushnew Robin Tarsiger
2020-12-04 21:51             ` Robin Tarsiger
2020-12-04 22:47         ` Sv: Ad-hoc list structure tutorializing (was: Lispref add-to-list - doc is unnecessary convoluted) arthur miller
2020-12-04 17:03     ` Lispref add-to-list - doc is unnecessary convoluted Drew Adams
2020-12-04 17:24       ` arthur miller
2020-12-04 18:02         ` Drew Adams

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=8ac1cf69-73ef-d8b9-df79-5a895f194936@dasyatidae.com \
    --to=rtt@dasyatidae.com \
    --cc=arthur.miller@live.com \
    --cc=emacs-devel@gnu.org \
    /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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).