Newsgroups: comp.lang.lisp
From: Kent M Pitman <pit...@world.std.com>
Date: 1999/01/27
Subject: Re: Sharing of list elements in Common Lisp
Barry already answered this and nothing he said is false,
but I have a different take on the nature of your question so consider this "additional data". > In Common Lisp, is it possible to have a list item shared Yes. This is not a property just of Lisp, by the way, but of any > in 2 or more lists; ie: > lista -> (a b c d) > listb -> (1 c 2 3) > where c is the same object? system which allows you to have pointers to objects with identity. In Lisp, everything is a pointer, and everything is an object with identity, so we don't use the word pointer except for emphasis--it is not, as in other languages, the introduction of an indirection. Conceptually, all objects are indirect to start with. As such, both of the above lists contain pointers to an abstract object (a symbol) c. > By same object, I mean This question is ill-formed. Deletion has nothing to do with > when c is deleted from one list, it disappears from the > other. an object, it has to do with the container to the object. The containers being different objects, it cannot be the case that deletion from one implies deletion from the other. If you literally shared the two container objects, of course, it could be. For example: (setq list0 '(b c d)) (setq list1 (cons 'a list0)) (setq list2 (cons 'a list0)) Now both list1 and list2 have c in them, but if you delete c from list0 it will also disappear from list1 and list2, but only because lists are recursive data structures and secretly the tail of list1 and the tail of list2 are the same object as list0. But if the objects were distinct, as in (setq list3 (cons 'a (copy-list list0))) then you would find that deleting c from list3 had no effect on list0 (or on list1 or list2). > It would also be desirable to preserve the linkage The linkage is preserved when you manipulate a list. Lists are > even if the item moves in one or both lists. about the object pointers in them, not about the recursive nature of those object pointers. Note that copy-list does not copy c. for example, The same is true of lists, which are objects. copy-list copies > Second, is it possible to record the position of Lisp is capable of implementing whatever storage structures any other > an item in a large list so the item may be efficently > retrieved? language can implement. The question you're asking is about philosophy and/or about algorithms. It is not about Lisp. That doesn't make it a bad question. But, if it helps, a good rule of thumb is that if it's something that's possible in general, Lisp can do it, and if it's something that is not possible in general, Lisp can't do it. There is some question of what you mean by "position" here. There is > Third, is it possible to get a "handle" or pointer to a Yes, and I've illustrated some above. But you should think about > list item which is independent of its position, or presence > in some other list? what you want to do that for because the answer is more specific to different situations. Is the position for lookup? If so, you don't need a list at all if you already have the object. Since it is a pointer, you can modify it directly and all lists containing it will see the update. Is the position for addition/deletion of siblings? In this case, you may want backpointers to the cons cell holding but you also may want a tree of some kind, not just a linear list. (It may be implemented with lists.) Are the positions for things that are finitely enumerable? For example, a list of the shift-keys on a character? Perhaps a bit table would be a better representation. The key thing to see in your question is that an object is itself Good luck. p.s. Disclaimer: I didn't try any of the examples above. You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||