unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: Marcin Borkowski <mbork@mbork.pl>,
	Rodrigo Morales <moralesrodrigo1100@gmail.com>
Cc: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Subject: RE: [External] : Re: [Elisp][Question] How to modify a list by index while preserving value outside of scope?
Date: Sun, 20 Aug 2023 21:45:59 +0000	[thread overview]
Message-ID: <SJ0PR10MB54889E72BF44F54EAF648A40F319A@SJ0PR10MB5488.namprd10.prod.outlook.com> (raw)
In-Reply-To: <87jztqjmsz.fsf@mbork.pl>

> In short: a "list" is in fact a "pointer", i.e., an "adress in memory"
> where the first cons of that list lives.

A NON-empty list is a cons, that is, a structure
with two pointers: to the list head and its tail.

An EMPTY list is NOT a cons; it's the symbol `nil',
which can also be written and read as `()'.

A true/proper list is one whose last tail is `nil'.

A dotted list is one whose last tail is not `nil'.
(It can be any value other than `nil'.)

Every non-empty list can be written using dot
notation: e.g., (a . (b . c)), whether or not c is
nil.  That's usually abbreviated as (a b . c), or
if c is nil, as (a b c).

> If you want to work on a "local" version of a list, you can copy that
> list yourself.  `append' is one way to do it;

`C-h f append' tells you:

 The last argument is NOT copied, just used as the
 tail of the new list.

This is important.  The tail of the result is last
argument.  So you can't copy a list by passing it
alone to `append':

 (let ((xs  (list 1 2 3))) (eq xs (append xs)))
 ; ==> t

But you can copy a list by appending nil to it:

 (let ((xs  (list 1 2 3))) (eq xs (append xs ())))
 ; ==> nil

> `cl-copy-list' is another.

Or `copy-sequence' or its alias `cl-copy-seq'.
___

You can think in terms of pointers, if that helps.
But Lisp doesn't have pointers in the usual sense
of the term.  You can't pass explicit pointers as
arguments, etc.  The "pointers" are used implicitly.

What's true is that when you pass a value, a _copy_
of the value is not created and passed.  So yes, a
pointer to the value is passed, but not explicitly
- not a pointer as such.  Such pointers are not
values/objects in the language.

In Lisp we think of the thing itself (which is a
Lisp value/object) as what is passed - not a copy,
and not a pointer to it.  The thing _is_ its
pointer (which of course is exactly what you said
in your first sentence).



      reply	other threads:[~2023-08-20 21:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-20  3:46 [Elisp][Question] How to modify a list by index while preserving value outside of scope? Rodrigo Morales
2023-08-20  4:08 ` Rodrigo Morales
2023-08-20  5:38 ` Marcin Borkowski
2023-08-20 21:45   ` Drew Adams [this message]

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=SJ0PR10MB54889E72BF44F54EAF648A40F319A@SJ0PR10MB5488.namprd10.prod.outlook.com \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=mbork@mbork.pl \
    --cc=moralesrodrigo1100@gmail.com \
    /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.
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).