aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman2006-07-24 17:54:38 +0000
committerRichard M. Stallman2006-07-24 17:54:38 +0000
commit3987272472307265e4c58669892a4841ce01733f (patch)
tree665ea3ffae8eedce566d23683001eec0f38bafa1
parent17f6554ce325389e53b182f95219fc3b927931b0 (diff)
downloademacs-3987272472307265e4c58669892a4841ce01733f.tar.gz
emacs-3987272472307265e4c58669892a4841ce01733f.zip
(List Variables): New node.
Material moved from other nodes.
-rw-r--r--lispref/lists.texi133
1 files changed, 119 insertions, 14 deletions
diff --git a/lispref/lists.texi b/lispref/lists.texi
index 4461ce54372..cb60baef900 100644
--- a/lispref/lists.texi
+++ b/lispref/lists.texi
@@ -20,6 +20,7 @@ the whole list.
20* List-related Predicates:: Is this object a list? Comparing two lists. 20* List-related Predicates:: Is this object a list? Comparing two lists.
21* List Elements:: Extracting the pieces of a list. 21* List Elements:: Extracting the pieces of a list.
22* Building Lists:: Creating list structure. 22* Building Lists:: Creating list structure.
23* List Variables:: Modifying lists stored in variables.
23* Modifying Lists:: Storing new pieces into an existing list. 24* Modifying Lists:: Storing new pieces into an existing list.
24* Sets And Lists:: A list can represent a finite mathematical set. 25* Sets And Lists:: A list can represent a finite mathematical set.
25* Association Lists:: A list can represent a finite relation or mapping. 26* Association Lists:: A list can represent a finite relation or mapping.
@@ -431,20 +432,6 @@ used in this example and the function named @code{list} described below;
431any symbol can serve both purposes. 432any symbol can serve both purposes.
432@end defun 433@end defun
433 434
434@defmac push newelt listname
435This macro provides an alternative way to write
436@code{(setq @var{listname} (cons @var{newelt} @var{listname}))}.
437
438@example
439(setq l '(a b))
440 @result{} (a b)
441(push 'c l)
442 @result{} (c a b)
443l
444 @result{} (c a b)
445@end example
446@end defmac
447
448@defun list &rest objects 435@defun list &rest objects
449This function creates a list with @var{objects} as its elements. The 436This function creates a list with @var{objects} as its elements. The
450resulting list is always @code{nil}-terminated. If no @var{objects} 437resulting list is always @code{nil}-terminated. If no @var{objects}
@@ -704,6 +691,124 @@ Some examples:
704@end example 691@end example
705@end defun 692@end defun
706 693
694@node List Variables
695@section Modifying List Variables
696
697 These functions, and one macro, provide convenient ways
698to modify a list which is stored in a variable.
699
700@defmac push newelt listname
701This macro provides an alternative way to write
702@code{(setq @var{listname} (cons @var{newelt} @var{listname}))}.
703
704@example
705(setq l '(a b))
706 @result{} (a b)
707(push 'c l)
708 @result{} (c a b)
709l
710 @result{} (c a b)
711@end example
712@end defmac
713
714 Two functions modify lists that are the values of variables.
715
716@defun add-to-list symbol element &optional append
717This function sets the variable @var{symbol} by consing @var{element}
718onto the old value, if @var{element} is not already a member of that
719value. It returns the resulting list, whether updated or not. The
720value of @var{symbol} had better be a list already before the call.
721Membership is tested using @code{equal}.
722
723Normally, if @var{element} is added, it is added to the front of
724@var{symbol}, but if the optional argument @var{append} is
725non-@code{nil}, it is added at the end.
726
727The argument @var{symbol} is not implicitly quoted; @code{add-to-list}
728is an ordinary function, like @code{set} and unlike @code{setq}. Quote
729the argument yourself if that is what you want.
730@end defun
731
732Here's a scenario showing how to use @code{add-to-list}:
733
734@example
735(setq foo '(a b))
736 @result{} (a b)
737
738(add-to-list 'foo 'c) ;; @r{Add @code{c}.}
739 @result{} (c a b)
740
741(add-to-list 'foo 'b) ;; @r{No effect.}
742 @result{} (c a b)
743
744foo ;; @r{@code{foo} was changed.}
745 @result{} (c a b)
746@end example
747
748 An equivalent expression for @code{(add-to-list '@var{var}
749@var{value})} is this:
750
751@example
752(or (member @var{value} @var{var})
753 (setq @var{var} (cons @var{value} @var{var})))
754@end example
755
756@defun add-to-ordered-list symbol element &optional order
757This function sets the variable @var{symbol} by inserting
758@var{element} into the old value, which must be a list, at the
759position specified by @var{order}. If @var{element} is already a
760member of the list, its position in the list is adjusted according
761to @var{order}. Membership is tested using @code{eq}.
762This function returns the resulting list, whether updated or not.
763
764The @var{order} is typically a number (integer or float), and the
765elements of the list are sorted in non-decreasing numerical order.
766
767@var{order} may also be omitted or @code{nil}. Then the numeric order
768of @var{element} stays unchanged if it already has one; otherwise,
769@var{element} has no numeric order. Elements without a numeric list
770order are placed at the end of the list, in no particular order.
771
772Any other value for @var{order} removes the numeric order of @var{element}
773if it already has one; otherwise, it is equivalent to @code{nil}.
774
775The argument @var{symbol} is not implicitly quoted;
776@code{add-to-ordered-list} is an ordinary function, like @code{set}
777and unlike @code{setq}. Quote the argument yourself if that is what
778you want.
779
780The ordering information is stored in a hash table on @var{symbol}'s
781@code{list-order} property.
782@end defun
783
784Here's a scenario showing how to use @code{add-to-ordered-list}:
785
786@example
787(setq foo '())
788 @result{} nil
789
790(add-to-ordered-list 'foo 'a 1) ;; @r{Add @code{a}.}
791 @result{} (a)
792
793(add-to-ordered-list 'foo 'c 3) ;; @r{Add @code{c}.}
794 @result{} (a c)
795
796(add-to-ordered-list 'foo 'b 2) ;; @r{Add @code{b}.}
797 @result{} (a b c)
798
799(add-to-ordered-list 'foo 'b 4) ;; @r{Move @code{b}.}
800 @result{} (a c b)
801
802(add-to-ordered-list 'foo 'd) ;; @r{Append @code{d}.}
803 @result{} (a c b d)
804
805(add-to-ordered-list 'foo 'e) ;; @r{Add @code{e}}.
806 @result{} (a c b e d)
807
808foo ;; @r{@code{foo} was changed.}
809 @result{} (a c b e d)
810@end example
811
707@node Modifying Lists 812@node Modifying Lists
708@section Modifying Existing List Structure 813@section Modifying Existing List Structure
709@cindex destructive list operations 814@cindex destructive list operations