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

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

Re: sharing list structure


From: Joe Corneli
Subject: Re: sharing list structure
Date: Thu, 24 Mar 2005 18:27:28 -0600

   > 
   > So I guess what I want is an "implicit pointer" to A.
   > 
   > Looking at the box diagrams in the manual, it seemed to me that
   > everything would be taken care of if I used "setcdr" to build the list
   > B.  But that didn't quite work:
   > 
   > (progn
   >   (setq A '(1 2 3))
   >   (setq B (list 'foo))
   >   (setcdr B A)
   >   (setq A (append A (list 4)))
   >   B)
   > ;=> (foo 1 2 3)

   The `append' doesn't alter the structure of the list A.

     (defvar *foo* (list 1 2 3))
     (append *foo* (list 4 5 6))
     *foo* => (1 2 3)

   Hence, the result of append doesn't alter A's structure.


Note the `setq' above, which make it look an awful lot like the
structure of A *is* being modified.  I mean, it comes out as a
different list --

(progn
  (setq A '(1 2 3))
  (setq B (list 'foo))
  (setcdr B A)
  (setq A (append A (list 4)))
  A)
;=> (1 2 3 4)

   > But is this the only way to go?  If it was possible, I would like to
   > set things up so that I could do anything I wanted to do to A, and
   > have B simply reflect that value at the end.

   You can do "anything you want" with A, as long as any function you run
   on A destructively modifies A. If it doesn't, then there's no way for
   B to reflect the change.

   So, just restrict yourself to destructive operations on A - like
   setcdr, setcar, etc. - and you'll be set. Just note that A will always
   have to be the "tail" part of B.

OK, I think I've got the idea now.  But still, I'm surprised that `setq'
is not among the list of "destructive functions".  What's that about?




reply via email to

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