unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Re: Appending lists
@ 2021-06-17  3:18 Drew Adams
  2021-06-17  7:48 ` tomas
  0 siblings, 1 reply; 32+ messages in thread
From: Drew Adams @ 2021-06-17  3:18 UTC (permalink / raw)
  To: Emanuel Berg, Help-Gnu-Emacs (help-gnu-emacs@gnu.org)

> >> What's a "deep copy"?
> >
> > It's a copy where every part of the object is separate, not
> > `eq' to the corresponding parts of the object that
> > you copied.
> >
> > For a cons, which is a tree, it would mean that neither car
> > nor cdr are shared between the original and the copy, and
> > that the same is true for any cars and cdrs that are,
> > themselves, conses.
> >
> > IOW, the tree is separate/independent from its copy.
> > When that's the case you can change any parts of either
> > without affecting the other.
> >
> > `C-h f copy-tree' describes the difference between what it
> > does (a deep copy) and what `copy-sequence' does (shallow
> > copy - doesn't copy cars.
> 
> deep copy = actual copy
> shallow copy = copy of references?

Did you read `C-h f copy-tree'?

No.  Everything involving list structure (conses)
uses references.

`copy-sequence' is a shallow copy - it copies only
the conses that make up the cdrs, so that you get
new conses for ONLY those - just the conses that
make up the _sequence_).

It copies only the flat, cdr list structure, not
any list structure in cars.  You get a new sequence,
but the sequence elements (the cars) share list
structure with the input sequence.

E.g., `copy-sequence' on the list `((1 2) 3 (4))'
gives you a new sequence (list) whose elements are
also `(1 2)', `3', and `(4)'.  The first and third
elements are not only `equal' to those of the input
sequence - they are `eq' to them.

`copy-tree' is a deep copy - it copies conses in
both cars and cdrs, so you get new list structure
(conses) everywhere.  There is NO cons in the
output that's `eq' to a cons in the input.

The result of `copy-tree' is a new `((1 2) 3 (4))'
list a new tree through and through.  Its first
element, `(1 2)' is not `eq' to the `(1 2)' of the
input list.  Same thing for its last element.

`copy-tree' results in a list that's `equal' to
the input list, but there is no cons in it that's
`eq' to a cons of the input.  `copy-sequence'
guarantees that conses in cars of the output
sequence ARE `eq' to those in the input.

(setq foo '((1 2) 3 (4)))

(setq sfoo (copy-sequence foo))

(eq foo sfoo)                 ; nil
(eq (car foo) (car sfoo))     ; t
(eq (caddr foo) (caddr sfoo)) ; t

(setq tfoo (copy-tree foo))

(eq foo tfoo)                 ; nil
(eq (car foo) (car tfoo))     ; nil
(eq (caddr foo) (caddr tfoo)) ; nil

`copy-tree' is more expensive.  `copy-sequence'
lets you continue to share sequence elements.

If two lists share any parts, then if you change
such a shared part in one it's changed also in
the other.  That's exactly the point of sharing.

It's also the "danger".  Don't share List
structure unless you intend to.  You may never
need to or want to.

You will need to be aware of list structure
and the possibility that it gets shared, when
playing with Lisp code that you didn't write,
at least.

And know about Lisp functions that can catch
you up on this kind of thing - functions called
"destructive" in the doc, i.e., functions that
can modify or share list structure.

____

[If you happen to know Fortran then think
COMMON blocks plus overlapping DIMENSION specs:
shared bits of overlapping memory, like Venn
diagrams.  Programs can act on different bits
in different ways.  Many large Fortran programs
were all about this sort of thing.  Likewise
some assembler programs.]




^ permalink raw reply	[flat|nested] 32+ messages in thread
* RE: [External] : Re: Appending lists
@ 2021-06-16 14:16 Drew Adams
  2021-06-16 17:03 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 32+ messages in thread
From: Drew Adams @ 2021-06-16 14:16 UTC (permalink / raw)
  To: Jean Louis, tomas@tuxteam.de; +Cc: help-gnu-emacs@gnu.org

> The variable does not change but its value apparently change

What does it mean for a variable's value to change?
Does it mean that the variable points to (returns)
a different value?  Or does it mean that the value
(itself) that it points to, changes?

Put differently, does a variable (itself) change
if it continues to point to the same Lisp object
(thingie) but that object changes?

If variable A evaluates to B at time T1 and later
A evaluates to C, has A's _value_ changed?  Sure.

But there are different ways in which that can
happen, and they can affect what one means.

 (setq A (cons 42 3)) ; A evals to (42 . 3)
 (setq A (cons 6 (cons 7 1))) ; A evals to (6 7 . 1)

That's very different, in terms of Lisp objects,
from this kind of change, though the value of A
is the same as in the previous example.

 (setq A (cons 42 3)) ; A evals to (42 . 3)
 (setcar A 6)
 (setcdr A (cons 7 1)) ; A evals to (6 7 . 1)

In the first case, first A points to one cons,
with car 42 and cdr 3.  Then A points to a
different cons, with car 6 and cdr (7 . 1).

In the second case, first A points to a cons
with car 42 and cdr 4.  Then A points to the
_same_ cons, but that cons now has car 6 and
cdr (7 . 1).

This is Lisp.  Lisp is _not_ a purely
functional language.  It's not even an
impurely functional language, you might say.

If you're using Lisp, it's good to know about
list structure, which (like it or not) is
modifiable.  You may not ever intentionally
modify list structure.  But it helps to be
aware of it.

LISP = LISt Processing language, where "list"
might not mean what you expect.

^ permalink raw reply	[flat|nested] 32+ messages in thread
* Re: Appending lists
@ 2021-06-14 16:36 Pierpaolo Bernardi
  2021-06-14 16:40 ` henri-biard
  0 siblings, 1 reply; 32+ messages in thread
From: Pierpaolo Bernardi @ 2021-06-14 16:36 UTC (permalink / raw)
  To: henri-biard@francemel fr, help-gnu-emacs@gnu org

Then

    (append city district)

does what you say you want. I suspect you want something else, though.

Il giorno 14 giugno 2021, alle ore 18:32, henri-biard@francemel.fr ha scritto:

I want to do 


  (append '(city district))


and put the new list into a new list.



From: Pierpaolo Bernardi <olopierpa@gmail.com>
To: henri-biard@francemel.fr <henri-biard@francemel.fr>;
   help-gnu-emacs@gnu org <help-gnu-emacs@gnu.org>
Subject: Re: Appending lists
Date: 14/06/2021 18:24:21 Europe/Paris

It is not clear what you want to do. Give an example of inputs and the relative output.

Il giorno 14 giugno 2021, alle ore 18:13, henri-biard@francemel.fr ha scritto:


I would like to make a list that is composed of appending three lists together.  Could use append,

but do not know how to put the result into a new list. 



(defvar city
  '( ("London" . 1)  ("Paris" . 2) ("Madrid" . 3))
   "docstring" )

(defvar district
   '( ("Greenwich" . 1) ("Louvre" . 2) ("Centro" . 3) )

   "docstring" )





^ permalink raw reply	[flat|nested] 32+ messages in thread

end of thread, other threads:[~2021-06-21  0:29 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-06-17  3:18 Appending lists Drew Adams
2021-06-17  7:48 ` tomas
2021-06-18 23:38   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-18 23:50   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-19  6:57     ` tomas
2021-06-19 21:31       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-19 22:07         ` [External] : " Drew Adams
2021-06-20 19:50           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-20 19:58             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-20 20:10               ` Jean Louis
2021-06-20 20:27                 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-20 19:59             ` Jean Louis
2021-06-20 20:22               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-20 20:42               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-20 20:49                 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-20 21:49                   ` Jean Louis
2021-06-20 22:54                     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-20 23:30                       ` Jean Louis
2021-06-21  0:29                         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-20 21:46                 ` Jean Louis
2021-06-20 22:09             ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-20 23:04               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-20 23:10                 ` Emanuel Berg via Users list for the GNU Emacs text editor
  -- strict thread matches above, loose matches on Subject: below --
2021-06-16 14:16 Drew Adams
2021-06-16 17:03 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-16 18:00   ` Drew Adams
2021-06-14 16:36 Pierpaolo Bernardi
2021-06-14 16:40 ` henri-biard
2021-06-14 18:21   ` Alexandr Vityazev
2021-06-15  0:46     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-15  8:27       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-15  9:18         ` tomas
2021-06-16  1:11           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-16  7:28             ` tomas
2021-06-16 16:42               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-16 16:55                 ` [External] : " Drew Adams
2021-06-16 17:06                   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-16 17:54                     ` Drew Adams
2021-06-16 23:49                       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-17  7:54                         ` tomas
2021-06-18 23:55                           ` Emanuel Berg via Users list for the GNU Emacs text editor

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