all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Teemu Likonen <tlikonen@iki.fi>
To: Boruch Baum <boruch_baum@gmx.com>,
	Emacs-Devel List <emacs-devel@gnu.org>
Subject: Re: inverse of add-to-list: remove-from-list
Date: Wed, 14 Oct 2020 08:28:15 +0300	[thread overview]
Message-ID: <878sc9fkb4.fsf@iki.fi> (raw)
In-Reply-To: <20201013193400.uqm2ng6rgfnsei2w@E15-2016.optimum.net>

[-- Attachment #1: Type: text/plain, Size: 2214 bytes --]

* 2020-10-13 15:34:00-04, Boruch Baum wrote:

>      (let ((aa '(1 2 3 1 4 1 2)))
>        (delete 2 aa)
>        aa)

In Lisp world it is not recommended to use destructive operations like
DELETE for literal objects like list created with QUOTE operator '(1 2)
or a string created with quotation marks ("abc"). Compilers can create a
single object for all literally defined object. Emacs Lisp manual
"(elisp) Equality Predicates" says:

    The Emacs Lisp byte compiler may collapse identical literal objects,
    such as literal strings, into references to the same object, with
    the effect that the byte-compiled code will compare such objects as
    ‘eq’, while the interpreted version of the same code will not.

For example:

    (defvar foo '(1 2 3))

    (let ((bar '(1 2 3)))
      (eq foo bar)               ; This may return T.
      (setq bar (delete 3 bar))) ; This may also modify FOO.

So, using a destructive operation for a literal object may change it
everywhere. To create a modifiable objects we should use LIST,
MAKE-LIST, MAKE-STRING, COPY-SEQUENCE, CL-COPY-SEQ, for example.

>    And note that the fine distinction made in the docstring for the
>    `delete' function:
>
>      "If SEQ is not a list, deletion is never performed destructively;
>       instead this function creates and returns a new vector or string.
>
>       Write ‘(setq foo (delete element foo))’ to be sure of correctly
>       changing the value of a sequence ‘foo’.
>
>    So, in the case of SEQ being a list, there is no need to `setq foo'.

In Lisp manuals "destructive" means that the operation may modify the
original object given as an argument. It doesn't promise anything else.
Sometimes the original object is modified, sometimes it is not. Even if
the original object is modified it doesn't promise that it's the same as
the return value of a destructive function. The original object and the
return value can be partially the same: lists may share some cons cells.
We must always use the return value of a destructive function.

-- 
/// Teemu Likonen - .-.. https://www.iki.fi/tlikonen/
// OpenPGP: 4E1055DC84E9DFF613D78557719D69D324539450

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 251 bytes --]

  parent reply	other threads:[~2020-10-14  5:28 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-13 19:34 inverse of add-to-list: remove-from-list Boruch Baum
2020-10-13  8:42 ` Uwe Brauer
2020-10-13  9:09   ` Andreas Schwab
2020-10-13  9:34     ` Thibaut Verron
2020-10-13 11:24       ` Uwe Brauer
2020-10-13 13:57   ` Stefan Monnier
2020-10-14  5:58     ` Thibaut Verron
2020-10-15 15:28       ` Uwe Brauer
2020-10-15 16:31       ` Stefan Monnier
2020-10-16 11:14         ` Thibaut Verron
2020-10-14  5:28   ` Teemu Likonen [this message]
2020-10-13 20:01 ` Andreas Schwab
2020-10-13 20:07   ` Boruch Baum
2020-10-13 20:41     ` Andreas Schwab

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=878sc9fkb4.fsf@iki.fi \
    --to=tlikonen@iki.fi \
    --cc=boruch_baum@gmx.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 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.