Next: , Previous: , Up: Sequence Functions   [Index]


E.3.6 Modifying List Variables

Here are convenient ways to modify a list stored in a variable.

Function: add-to-list symbol element &optional append compare-fn

This function sets the variable SYMBOL by consing ELEMENT onto the old value, if ELEMENT is not already a member of that value. It returns the resulting list, whether updated or not. The value of SYMBOL had better be a list already before the call. ‘add-to-list’ uses COMPARE-FN to compare ELEMENT against existing list members; if COMPARE-FN is ‘nil’, it uses ‘equal’.

Normally, if ELEMENT is added, it is added to the front of SYMBOL, but if the optional argument APPEND is non-‘nil’, it is added at the end.

The argument SYMBOL is not implicitly quoted; ‘add-to-list’ is an ordinary function, like ‘set’ and unlike ‘setq’. Quote the argument yourself if that is what you want.

Function: add-to-ordered-list symbol element &optional order

This function sets the variable SYMBOL by inserting ELEMENT into the old value, which must be a list, at the position specified by ORDER. If ELEMENT is already a member of the list, its position in the list is adjusted according to ORDER. Membership is tested using ‘eq’. This function returns the resulting list, whether updated or not.