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

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

Re: Add-to-list or setq append...


From: Tim X
Subject: Re: Add-to-list or setq append...
Date: Wed, 08 Dec 2010 15:22:44 -0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

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


reply via email to

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