* Add-to-list or setq append... @ 2010-08-03 22:37 Andrea Crotti 2010-08-03 22:41 ` Richard Riley [not found] ` <mailman.5.1280875308.9029.help-gnu-emacs@gnu.org> 0 siblings, 2 replies; 12+ messages in thread From: Andrea Crotti @ 2010-08-03 22:37 UTC (permalink / raw) To: help-gnu-emacs Just a curiosity, I was wondering why in so many README and doc they suggest to add elements to lists with something like: --8<---------------cut here---------------start------------->8--- (setq list (append list element)) --8<---------------cut here---------------end--------------->8--- instead of doing simply --8<---------------cut here---------------start------------->8--- (add-to-list list-va element) --8<---------------cut here---------------end--------------->8--- Is there any good reason or is just historical heritage? I think add-to-list is even more smart adding only if not there already, then what's the point in using that ugly form? ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Add-to-list or setq append... 2010-08-03 22:37 Add-to-list or setq append Andrea Crotti @ 2010-08-03 22:41 ` Richard Riley 2010-08-04 0:06 ` Andrea Crotti [not found] ` <mailman.5.1280875308.9029.help-gnu-emacs@gnu.org> 1 sibling, 1 reply; 12+ messages in thread From: Richard Riley @ 2010-08-03 22:41 UTC (permalink / raw) To: help-gnu-emacs Andrea Crotti <andrea.crotti.0@gmail.com> writes: > Just a curiosity, I was wondering why in so many README and doc they > suggest to add elements to lists with something like: > > > > (setq list (append list element)) > > > > instead of doing simply > > > (add-to-list list-va element) > > > > Is there any good reason or is just historical heritage? > I think add-to-list is even more smart adding only if not there already, > then what's the point in using that ugly form? > With append you can append a list. e.g (setq load-path (append load-path '("/usr/local/emacs/site-lisp" "/usr/share/emacs/site-lisp"))) I don't know if add-to-list can do that? ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Add-to-list or setq append... 2010-08-03 22:41 ` Richard Riley @ 2010-08-04 0:06 ` Andrea Crotti 2010-08-04 0:35 ` Drew Adams 0 siblings, 1 reply; 12+ messages in thread From: Andrea Crotti @ 2010-08-04 0:06 UTC (permalink / raw) To: help-gnu-emacs Richard Riley <rileyrg@gmail.com> writes: > With append you can append a list. e.g > > (setq load-path (append load-path '("/usr/local/emacs/site-lisp" "/usr/share/emacs/site-lisp"))) > > I don't know if add-to-list can do that? Sure but I was talking about simpler cases, for example --8<---------------cut here---------------start------------->8--- (setq auto-mode-alist (cons '("\\.ml\\w?" . tuareg-mode) auto-mode-alist)) --8<---------------cut here---------------end--------------->8--- is equivalent to --8<---------------cut here---------------start------------->8--- (add-to-list 'auto-mode-alist '("\\.ml\\w?" . tuareg-mode)) --8<---------------cut here---------------end--------------->8--- right? Maybe some years ago add-to-list was not present? ^ permalink raw reply [flat|nested] 12+ messages in thread
* RE: Add-to-list or setq append... 2010-08-04 0:06 ` Andrea Crotti @ 2010-08-04 0:35 ` Drew Adams 2010-08-04 8:02 ` Andrea Crotti 0 siblings, 1 reply; 12+ messages in thread From: Drew Adams @ 2010-08-04 0:35 UTC (permalink / raw) To: 'Andrea Crotti', help-gnu-emacs > (setq auto-mode-alist (cons '("\\.ml\\w?" . tuareg-mode) auto-mode-alist)) > is equivalent to > (add-to-list 'auto-mode-alist '("\\.ml\\w?" . tuareg-mode)) No, it is not. The former unconditionally adds the entry at the beginning of the alist. The latter adds the entry to the alist only if an `equal' entry is not already present. The former ensures that the entry is at the start of the list. The latter does not. In the former case the alist can end up with multiple entries with the given key, the first such entry shadowing all others with the same key. If you then pop off that first entry, the previously first of all entries with that key is still present and now becomes the one that matters (shadows any others with that key). ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Add-to-list or setq append... 2010-08-04 0:35 ` Drew Adams @ 2010-08-04 8:02 ` Andrea Crotti 2010-08-04 12:57 ` Andrea Crotti ` (2 more replies) 0 siblings, 3 replies; 12+ messages in thread From: Andrea Crotti @ 2010-08-04 8:02 UTC (permalink / raw) To: help-gnu-emacs "Drew Adams" <drew.adams@oracle.com> writes: > > No, it is not. The former unconditionally adds the entry at the beginning of > the alist. The latter adds the entry to the alist only if an `equal' entry is > not already present. > > The former ensures that the entry is at the start of the list. The latter does > not. > > In the former case the alist can end up with multiple entries with the given > key, the first such entry shadowing all others with the same key. If you then > pop off that first entry, the previously first of all entries with that key is > still present and now becomes the one that matters (shadows any others with that > key). Sure but --8<---------------cut here---------------start------------->8--- ELISP> my-l ((1 . 3) (1 . 2)) ELISP> (add-to-list 'my-l '(2 . 3)) ((2 . 3) (1 . 3) (1 . 2)) --8<---------------cut here---------------end--------------->8--- it actually not add it ONLY if they're really equal, which is desirable in my opinion. What's the point in having twice the same cons in auto-mode-alist? Then the real difference is that I add in the end instead of at the beginning, and in that case I don't shadow an already present value... ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Add-to-list or setq append... 2010-08-04 8:02 ` Andrea Crotti @ 2010-08-04 12:57 ` Andrea Crotti 2010-08-04 15:41 ` Drew Adams [not found] ` <mailman.2.1280926672.16887.help-gnu-emacs@gnu.org> 2010-08-04 15:42 ` Drew Adams 2 siblings, 1 reply; 12+ messages in thread From: Andrea Crotti @ 2010-08-04 12:57 UTC (permalink / raw) To: help-gnu-emacs Andrea Crotti <andrea.crotti.0@gmail.com> writes: > it actually not add it ONLY if they're really equal, which is desirable > in my opinion. > What's the point in having twice the same cons in auto-mode-alist? > > Then the real difference is that I add in the end instead of at the > beginning, and in that case I don't shadow an already present value... And also why not using some kind of union when merging two lists. There is a union defined in cl-seq, that for example would suffice the ugly double elements in the same list... ^ permalink raw reply [flat|nested] 12+ messages in thread
* RE: Add-to-list or setq append... 2010-08-04 12:57 ` Andrea Crotti @ 2010-08-04 15:41 ` Drew Adams 0 siblings, 0 replies; 12+ messages in thread From: Drew Adams @ 2010-08-04 15:41 UTC (permalink / raw) To: 'Andrea Crotti', help-gnu-emacs > And also why not using some kind of union when merging two lists. > There is a union defined in cl-seq, that for example would suffice the > ugly double elements in the same list... Why not take the bus to Milwaukee? Why not collect stamps? You use a set union function when you want a set union. End of story. You can use a list for anything. Sometimes you want duplicate elements; sometimes you do not; sometimes you don't care. Sometimes you want the elements to be sorted and remain sorted. Sometimes you do not. It all depends on your needs. ^ permalink raw reply [flat|nested] 12+ messages in thread
[parent not found: <mailman.2.1280926672.16887.help-gnu-emacs@gnu.org>]
* Re: Add-to-list or setq append... [not found] ` <mailman.2.1280926672.16887.help-gnu-emacs@gnu.org> @ 2010-08-04 13:33 ` Pascal J. Bourguignon 0 siblings, 0 replies; 12+ messages in thread From: Pascal J. Bourguignon @ 2010-08-04 13:33 UTC (permalink / raw) To: help-gnu-emacs Andrea Crotti <andrea.crotti.0@gmail.com> writes: > Andrea Crotti <andrea.crotti.0@gmail.com> writes: > >> it actually not add it ONLY if they're really equal, which is desirable >> in my opinion. >> What's the point in having twice the same cons in auto-mode-alist? >> >> Then the real difference is that I add in the end instead of at the >> beginning, and in that case I don't shadow an already present value... > > And also why not using some kind of union when merging two lists. > There is a union defined in cl-seq, that for example would suffice the > ugly double elements in the same list... If union in cl-seq is implemented as specified by clhs, it doesn't ensure that the resulting list will have unique elements (for the given union criteria, which by the way, when involving :test-not (inequalities in general) is rather hairy and un-obvious). What is ensured is that for each pair (r,s) in R×S, only one of the two elements of is in the result, which doesn't mean much, because the other element may be included in the result because of the result of the test on another pair. If you want a list with unique elements, you should use remove-duplicates. -- __Pascal Bourguignon__ http://www.informatimago.com/ ^ permalink raw reply [flat|nested] 12+ messages in thread
* RE: Add-to-list or setq append... 2010-08-04 8:02 ` Andrea Crotti 2010-08-04 12:57 ` Andrea Crotti [not found] ` <mailman.2.1280926672.16887.help-gnu-emacs@gnu.org> @ 2010-08-04 15:42 ` Drew Adams 2 siblings, 0 replies; 12+ messages in thread From: Drew Adams @ 2010-08-04 15:42 UTC (permalink / raw) To: 'Andrea Crotti', help-gnu-emacs > ELISP> my-l > ((1 . 3) > (1 . 2)) > > ELISP> (add-to-list 'my-l '(2 . 3)) > ((2 . 3) > (1 . 3) > (1 . 2)) > > it actually not add it ONLY if they're really equal, which is > desirable in my opinion. `add-to-list' adds an element ONLY if it is NOT `equal' to any existing element. `add-to-list' is for any list, not just an alist. `equal is used to test whether an _entry_ is already present in the list. `equal' is applied to the potentially new entry and to each list entry (aka item, element). This is not a comparison of the cars or cdrs of the entry; it is a comparison of the entry as a whole: both its car and cdr. The cons (2 . 3) is not `equal' to any item in the list, so it is added. > What's the point in having twice the same cons in auto-mode-alist? None. I misspoke in mentioning shadowing wrt the examples in your second mail. I was thinking of something like this, where only the alist keys are compared: (add-to-list 'auto-mode-alist '("\\.ml\\w?" . tuareg-mode) nil (lambda (x y) (equal (car x) (car y)))) That would allow for only one entry with the given key. I was speaking to why you might want to allow more than one such - which I think got closer to your original question. Your original question was not just about `auto-mode-alist'. You asked why someone might use (setq list (append (list element))) instead of (add-to-list list-va element). Any list can be used for anything you like. Multiple occurrences of `equal' items or even `eq' items can often be useful. It is often the case in using alists, in particular, that you do not care whether there are also other items with the same key, because only the first one is seen (using the usual alist operations). If you don't care about removing duplicates (wrt the key) then, well, you don't care about removing them. And in some cases, as I said, you want to preserve the older alist entries so that you can retrieve them or make them active at some point. You might want to use the alist as a stack of definitions or in some other way where old items can be useful. One program, in one context, adds an entry to `auto-mode-alist'. In a different context, another adds an entry that shadows the first one (it has the same key). Upon leaving the second context you might well want to make the previous association active again. Removing the shadowing entry is one way to do that. If the original entry was deleted then you cannot retrieve it (but you could add it again). > Then the real difference is that I add in the end instead of at the > beginning, That is not the real difference. That is not even a difference - you can add at either the end or the beginning with either `add-to-list' or `setq'. The real differences are those I outlined previously. When I said that `add-to-list' does not assure that the entry is at the start of the list I was referring to the case where the entry is already present somewhere down the list, so it is not added. In that case, it is not at the head of the list. You might or might not care that the item is first in the list. It all depends how you use the list. > and in that case I don't shadow an already present value... You never shadow an `equal' entry with a new one if you use `add-to-list' - there are no occurences of `equal' items. If you in some other way add an `equal' item at the end or otherwise after some `equal' occurrence in the list, it is itself shadowed. You can do anything you want with a list. You can put an item at any position, including an item that is `equal' to some other item. You can put it in any position relative to any number of such `equal' items, thus determining its "shadowing depth" (for lack of a better term), which determines at what point it would be unshadowed if you pop items of the list head. Both of the cliches you showed are useful (`add-to-list', `setq'). They are different. ^ permalink raw reply [flat|nested] 12+ messages in thread
[parent not found: <mailman.5.1280875308.9029.help-gnu-emacs@gnu.org>]
* Re: Add-to-list or setq append... [not found] ` <mailman.5.1280875308.9029.help-gnu-emacs@gnu.org> @ 2010-08-04 18:24 ` David Kastrup 0 siblings, 0 replies; 12+ messages in thread From: David Kastrup @ 2010-08-04 18:24 UTC (permalink / raw) To: help-gnu-emacs Richard Riley <rileyrg@gmail.com> writes: > Andrea Crotti <andrea.crotti.0@gmail.com> writes: > >> Just a curiosity, I was wondering why in so many README and doc they >> suggest to add elements to lists with something like: >> >> >> >> (setq list (append list element)) >> >> >> >> instead of doing simply >> >> >> (add-to-list list-va element) >> >> >> >> Is there any good reason or is just historical heritage? >> I think add-to-list is even more smart adding only if not there already, >> then what's the point in using that ugly form? > > With append you can append a list. e.g > > (setq load-path (append load-path '("/usr/local/emacs/site-lisp" "/usr/share/emacs/site-lisp"))) > > I don't know if add-to-list can do that? It would have taken you less time to ask Emacs than it takes a hundred readers to read your unnecessary message. C-h f add-to-list RET add-to-list is a compiled Lisp function in `subr.el'. (add-to-list LIST-VAR ELEMENT &optional APPEND COMPARE-FN) [...] If ELEMENT is added, it is added at the beginning of the list, unless the optional argument APPEND is non-nil, in which case ELEMENT is added at the end. [...] -- David Kastrup ^ permalink raw reply [flat|nested] 12+ messages in thread
[parent not found: <mailman.3.1280875040.9029.help-gnu-emacs@gnu.org>]
* Re: Add-to-list or setq append... [not found] <mailman.3.1280875040.9029.help-gnu-emacs@gnu.org> @ 2010-08-04 1:02 ` Pascal J. Bourguignon 2010-08-04 8:12 ` Tim X 1 sibling, 0 replies; 12+ messages in thread From: Pascal J. Bourguignon @ 2010-08-04 1:02 UTC (permalink / raw) To: help-gnu-emacs Andrea Crotti <andrea.crotti.0@gmail.com> writes: > Just a curiosity, I was wondering why in so many README and doc they > suggest to add elements to lists with something like: > > --8<---------------cut here---------------start------------->8--- > (setq list (append list element)) > --8<---------------cut here---------------end--------------->8--- > > instead of doing simply > --8<---------------cut here---------------start------------->8--- > (add-to-list list-va element) > --8<---------------cut here---------------end--------------->8--- > > Is there any good reason or is just historical heritage? > I think add-to-list is even more smart adding only if not there already, > then what's the point in using that ugly form? I would rather use: (require 'cl) (pushnew element list-va) than add-to-list. -- __Pascal Bourguignon__ http://www.informatimago.com/ ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Add-to-list or setq append... [not found] <mailman.3.1280875040.9029.help-gnu-emacs@gnu.org> 2010-08-04 1:02 ` Pascal J. Bourguignon @ 2010-08-04 8:12 ` Tim X 1 sibling, 0 replies; 12+ messages in thread From: Tim X @ 2010-08-04 8:12 UTC (permalink / raw) To: help-gnu-emacs Andrea Crotti <andrea.crotti.0@gmail.com> writes: > Just a curiosity, I was wondering why in so many README and doc they > suggest to add elements to lists with something like: > > (setq list (append list element)) > > instead of doing simply > (add-to-list list-va element) > > Is there any good reason or is just historical heritage? > I think add-to-list is even more smart adding only if not there already, > then what's the point in using that ugly form? > Possibly a bit of both. Both forms have their use. Some will likely argue that setq is more general and can be used to add items at the end via append or at the beginning using cons etc. However, they a probably not aware that add-to-list, by default adds the element at the beginning of the list, but has an optional argument to add it at the end. some will argue that add-to-list only does an 'equal' test to determine if the element is already in the list, but this too can be changed with an optional argument. One area where add-to-list is weak is when you want to add the element to a specific location that is not at the start or end of the list. The other point to note is that add-to-list modifies its list argument i.e. is destructive. (append l v) creates a new list and does not modify l, which is why you need the setq. Frequently, you see append, cons etc because it is more 'lispish'. Back when many places used lisp and scheme to teach, many new emacs users came to elisp having already learnt a lisp. For them, append, cons, push etc were/are more familiar idioms while add-to-list is more dialect specific. Tim -- tcross (at) rapttech dot com dot au ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2010-08-04 18:24 UTC | newest] Thread overview: 12+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2010-08-03 22:37 Add-to-list or setq append Andrea Crotti 2010-08-03 22:41 ` Richard Riley 2010-08-04 0:06 ` Andrea Crotti 2010-08-04 0:35 ` Drew Adams 2010-08-04 8:02 ` Andrea Crotti 2010-08-04 12:57 ` Andrea Crotti 2010-08-04 15:41 ` Drew Adams [not found] ` <mailman.2.1280926672.16887.help-gnu-emacs@gnu.org> 2010-08-04 13:33 ` Pascal J. Bourguignon 2010-08-04 15:42 ` Drew Adams [not found] ` <mailman.5.1280875308.9029.help-gnu-emacs@gnu.org> 2010-08-04 18:24 ` David Kastrup [not found] <mailman.3.1280875040.9029.help-gnu-emacs@gnu.org> 2010-08-04 1:02 ` Pascal J. Bourguignon 2010-08-04 8:12 ` Tim X
Code repositories for project(s) associated with this external index https://git.savannah.gnu.org/cgit/emacs.git https://git.savannah.gnu.org/cgit/emacs/org-mode.git This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.