help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Appending lists


From: tomas
Subject: Re: Appending lists
Date: Thu, 17 Jun 2021 09:48:43 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

On Thu, Jun 17, 2021 at 03:18:48AM +0000, Drew Adams wrote:
> > >> What's a "deep copy"?

[...]

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

All this shows that "shallow" and "deep" are relative.
Copy-tree is "as deep as it gets", with some consequences.

Let's do an experiment, shall we?

But before: save all your texts. And, if Emacs behaves
strangely, C-g usually gets you out of the thickets.

So put on your lab coat and your security goggles. For an
awesome experience, best do your session interactive, step
by step:

  (setq lst '(a b c d dacapo))
  => (a b c d dacapo)
  
  (cddddr lst)
  => (dacapo) ;;; OK, it's the last one.
  
  (setcdr (cddddr lst) lst)) ;;; make circular list [1]
  => (a b c d dacapo a b c d dacapo a . #5) ;;; uh, whatever (see below [2])
  
  lst
  => (a b c d dacapo a b c d dacapo a . #5) ;;; (see below [2])
  
  (setq otherlst (copy-tree lst)) ;;; TIGHTEN YOUR GOGGLES!
  => ... ;;; [3]

As I said above: you (most probably) can get your Emacs back with C-g.

Cheers

[1] https://en.wikipedia.org/wiki/Ouroboros
[2] We have a circular list. This funny #5 you see there is print's
   way to tell you "hey, I've been here already. Are you trying to
   pull my leg?". Its behaviour depends on the variable `print-circle',
   which, when true, makes print even smarter -- in our case it's
   nil, but it seems that for simple cases like this one, print
   sometimes doesn't explode. Hold on to your goggles at step 3,
   just in case...
[3] Copy-tree, alas, isn't as smart. It will happily fill your memory
   if you don't hurry up with your C-g.

 - tomás

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]