list.tex 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. \chapter{Lists}
  2. A list\index{List} is an object consisting of a sequence of other objects
  3. (including lists themselves), separated by commas and surrounded by
  4. braces. Examples of lists are:
  5. \begin{verbatim}
  6. {a,b,c}
  7. {1,a-b,c=d}
  8. {{a},{{b,c},d},e}.
  9. \end{verbatim}
  10. The empty list is represented as
  11. \begin{verbatim}
  12. {}.
  13. \end{verbatim}
  14. \section{Operations on Lists}\index{List operation}
  15. Several operators in the system return their results as lists, and a user
  16. can create new lists using braces and commas. Alternatively, one can use
  17. the operator LIST to construct a list. An important class of operations
  18. on lists are MAP and SELECT operations. For details, please refer to the
  19. chapters on MAP, SELECT and the FOR command. See also the documentation
  20. on the ASSIST package.
  21. To facilitate the use of
  22. lists, a number of operators are also available for manipulating
  23. them. {\tt PART(<list>,n)}\ttindex{PART} for example will return the
  24. $n^{th}$ element of a list. {\tt LENGTH}\ttindex{LENGTH} will return the
  25. length of a list. Several operators are also defined uniquely for lists.
  26. For those familiar with them, these operators in fact mirror the
  27. operations defined for Lisp lists. These operators are as follows:
  28. \subsection{LIST}
  29. The operator LIST is an alternative to the usage of curly brackets. LIST
  30. accepts an arbitrary number of arguments and returns a list
  31. of its arguments. This operator is useful in cases where operators
  32. have to be passed as arguments. E.g.,
  33. \begin{verbatim}
  34. list(a,list(list(b,c),d),e); -> {{a},{{b,c},d},e}
  35. \end{verbatim}
  36. \subsection{FIRST}
  37. This operator\ttindex{FIRST} returns the first member of a list. An error
  38. occurs if the argument is not a list, or the list is empty.
  39. \subsection{SECOND}
  40. {\tt SECOND}\ttindex{SECOND} returns the second member of a list. An error
  41. occurs if the argument is not a list or has no second element.
  42. \subsection{THIRD}
  43. This operator\ttindex{THIRD} returns the third member of a list. An error
  44. occurs if the argument is not a list or has no third element.
  45. \subsection{REST}
  46. {\tt REST}\ttindex{REST} returns its argument with the first element
  47. removed. An error occurs if the argument is not a list, or is empty.
  48. \subsection{$.$ (Cons) Operator}
  49. This operator\ttindex{. (CONS)} adds (``conses'') an expression to the
  50. front of a list. For example:
  51. \begin{verbatim}
  52. a . {b,c} -> {a,b,c}.
  53. \end{verbatim}
  54. \subsection{APPEND}
  55. This operator\ttindex{APPEND} appends its first argument to its second to
  56. form a new list.
  57. {\it Examples:}
  58. \begin{verbatim}
  59. append({a,b},{c,d}) -> {a,b,c,d}
  60. append({{a,b}},{c,d}) -> {{a,b},c,d}.
  61. \end{verbatim}
  62. \subsection{REVERSE}
  63. The operator {\tt REVERSE}\ttindex{REVERSE} returns its argument with the
  64. elements in the reverse order. It only applies to the top level list, not
  65. any lower level lists that may occur. Examples are:\index{List operation}
  66. \begin{verbatim}
  67. reverse({a,b,c}) -> {c,b,a}
  68. reverse({{a,b,c},d}) -> {d,{a,b,c}}.
  69. \end{verbatim}
  70. \subsection{List Arguments of Other Operators}
  71. If an operator other than those specifically defined for lists is given a
  72. single argument that is a list, then the result of this operation will be
  73. a list in which that operator is applied to each element of the list. For
  74. example, the result of evaluating {\tt log\{a,b,c\}} is the expression
  75. {\tt \{LOG(A),LOG(B),LOG(C)\}}.
  76. There are two ways to inhibit this operator distribution. Firstly, the
  77. switch {\tt LISTARGS},\ttindex{LISTARGS} if on, will globally inhibit
  78. such distribution. Secondly, one can inhibit this distribution for a
  79. specific operator by the declaration {\tt LISTARGP}.\ttindex{LISTARGP} For
  80. example, with the declaration {\tt listargp log}, {\tt log\{a,b,c\}} would
  81. evaluate to {\tt LOG(\{A,B,C\})}.
  82. If an operator has more than one argument, no such distribution occurs.
  83. \subsection{Caveats and Examples}
  84. Some of the natural list operations such as {\it member} or {\it delete}
  85. are available only after loading the package {\it ASSIST}.
  86. Please note that a non-list as second argument to CONS
  87. (a "dotted pair" in LISP terms) is not allowed
  88. and causes an "invalid as list" error.
  89. \begin{verbatim}
  90. a := 17 . 4;
  91. ***** 17 4 invalid as list
  92. \end{verbatim}
  93. Also, the initialization of a scalar variable is not the empty list --
  94. one has to set list type variables explicitly, as in the following
  95. example:
  96. \begin{verbatim}
  97. load_package assist;
  98. procedure lotto (n,m);
  99. begin scalar list_1_n, luckies, hit;
  100. list_1_n := {};
  101. luckies := {};
  102. for k:=1:n do list_1_n := k . list_1_n;
  103. for k:=1:m do
  104. << hit := part(list_1_n,random(n-k+1) + 1);
  105. list_1_n := delete(hit,list_1_n);
  106. luckies := hit . luckies >>;
  107. return luckies;
  108. end; % In Germany, try lotto (49,6);
  109. \end{verbatim}
  110. {\it Another example:} Find all coefficients of a multivariate
  111. polynomial with respect to a list of variables:
  112. \begin{verbatim}
  113. procedure allcoeffs(q,lis); % q : polynomial, lis: list of vars
  114. allcoeffs1 (list q,lis);
  115. procedure allcoeffs1(q,lis);
  116. if lis={} then q else
  117. allcoeffs1(foreach qq in q join coeff(qq,first lis),rest lis);
  118. \end{verbatim}