1 @c -----------------------------------------------------------------------------
3 @node Lists, Arrays, Constants, Data Types and Structures
5 @c -----------------------------------------------------------------------------
8 * Introduction to Lists::
9 * Functions and Variables for Lists::
10 * Performance considerations for Lists::
13 @c -----------------------------------------------------------------------------
14 @node Introduction to Lists, Functions and Variables for Lists, Lists, Lists
15 @subsection Introduction to Lists
16 @c -----------------------------------------------------------------------------
18 Lists are the basic building block for Maxima and Lisp. All data types
19 other than arrays, @mref{hashed arrays} and numbers are represented as Lisp lists,
20 These Lisp lists have the form
27 to indicate an expression @code{a+2}. At Maxima level one would see
28 the infix notation @code{a+2}. Maxima also has lists which are printed
36 for a list with 4 elements. Internally this corresponds to a Lisp list
40 ((MLIST) 1 2 7 ((MPLUS) $X $Y))
44 The flag which denotes the type field of the Maxima expression is a list
45 itself, since after it has been through the simplifier the list would become
48 ((MLIST SIMP) 1 2 7 ((MPLUS SIMP) $X $Y))
51 @c -----------------------------------------------------------------------------
52 @node Functions and Variables for Lists, Performance considerations for Lists ,Introduction to Lists, Lists
53 @subsection Functions and Variables for Lists
54 @c -----------------------------------------------------------------------------
56 @c -----------------------------------------------------------------------------
59 @fnindex List delimiters
60 @fnindex Subscript operator
65 @code{[} and @code{]} mark the beginning and end, respectively, of a list.
67 @code{[} and @code{]} also enclose the subscripts of
68 a list, array, @mrefcomma{hashed array} or @mrefdot{memoizing function} Note that
69 other than for arrays accessing the @code{n}th element of a list
70 may need an amount of time that is roughly proportional to @code{n},
71 @xref{Performance considerations for Lists}.
73 Note that if an element of a subscripted variable is written to before
74 a list or an array of this name is declared a @mref{hashed array}
75 (@pxref{Arrays}) is created, not a list.
82 @c array (y, fixnum, 3);
100 (%i3) array (y, fixnum, 3);
120 (%i8) g[k] := 1/(k^2+1);
134 @opencatbox{Categories:}
140 @c NEED ANOTHER deffn FOR GENERAL EXPRESSIONS ARGUMENTS
141 @c NEEDS CLARIFICATION AND EXAMPLES
143 @c -----------------------------------------------------------------------------
145 @deffn {Function} append (@var{list_1}, @dots{}, @var{list_n})
147 Returns a single list of the elements of @var{list_1} followed
148 by the elements of @var{list_2}, @dots{} @code{append} also works on
149 general expressions, e.g. @code{append (f(a,b), f(c,d,e));} yields
152 See also @mrefcomma{addrow} @mref{addcol} and @mrefdot{join}
154 Do @code{example(append);} for an example.
156 @opencatbox{Categories:}
158 @category{Expressions}
162 @c NEEDS CLARIFICATION AND EXAMPLES
164 @c -----------------------------------------------------------------------------
166 @deffn {Function} assoc @
167 @fname{assoc} (@var{key}, @var{e}, @var{default}) @
168 @fname{assoc} (@var{key}, @var{e})
170 @code{assoc} searches for @var{key} as the first part of an argument of @var{e}
171 and returns the second part of the first match, if any.
173 @var{key} may be any expression.
174 @var{e} must be a nonatomic expression,
175 and every argument of @var{e} must have exactly two parts.
176 @code{assoc} returns the second part of the first matching argument of @var{e}.
177 Matches are determined by @code{is(@var{key} = first(@var{a}))}
178 where @var{a} is an argument of @var{e}.
180 If there are two or more matches, only the first is returned.
181 If there are no matches, @var{default} is returned, if specified.
182 Otherwise, @code{false} is returned.
184 See also @mref{sublist} and @mrefdot{member}
188 @var{key} may be any expression.
189 @var{e} must be a nonatomic expression,
190 and every argument of @var{e} must have exactly two parts.
191 @code{assoc} returns the second part of the first matching argument of @var{e}.
194 @c assoc (f(x), foo(g(x) = y, f(x) = z + 1, h(x) = 2*u));
198 (%i1) assoc (f(x), foo(g(x) = y, f(x) = z + 1, h(x) = 2*u));
203 If there are two or more matches, only the first is returned.
206 @c assoc (yy, [xx = 111, yy = 222, yy = 333, yy = 444]);
210 (%i1) assoc (yy, [xx = 111, yy = 222, yy = 333, yy = 444]);
215 If there are no matches, @var{default} is returned, if specified.
216 Otherwise, @code{false} is returned.
219 @c assoc (abc, [[x, 111], [y, 222], [z, 333]], none);
220 @c assoc (abc, [[x, 111], [y, 222], [z, 333]]);
224 (%i1) assoc (abc, [[x, 111], [y, 222], [z, 333]], none);
226 (%i2) assoc (abc, [[x, 111], [y, 222], [z, 333]]);
231 @opencatbox{Categories:}
233 @category{Expressions}
238 @c -----------------------------------------------------------------------------
240 @deffn {Function} cons @
241 @fname{cons} (@var{expr}, @var{list}) @
242 @fname{cons} (@var{expr_1}, @var{expr_2})
244 @code{cons (@var{expr}, @var{list})} returns a new list constructed of the element
245 @var{expr} as its first element, followed by the elements of @var{list}. This is
246 analogous to the Lisp language construction operation "cons".
248 The Maxima function @code{cons} can also be used where the second argument is other
249 than a list and this might be useful. In this case, @code{cons (@var{expr_1}, @var{expr_2})}
250 returns an expression with same operator as @var{expr_2} but with argument @code{cons(expr_1, args(expr_2))}.
259 (%i1) cons(a,[b,c,d]);
263 (%i2) cons(a,f(b,c,d));
268 In general, @code{cons} applied to a nonlist doesn't make sense. For instance, @code{cons(a,b^c)}
269 results in an illegal expression, since '^' cannot take three arguments.
271 When @code{inflag} is true, @code{cons} operates on the internal structure of an expression, otherwise
272 @code{cons} operates on the displayed form. Especially when @code{inflag} is true, @code{cons} applied
273 to a nonlist sometimes gives a surprising result; for example
276 @c cons(a,-a), inflag : true;
277 @c cons(a,-a), inflag : false;
281 (%i1) cons(a,-a), inflag : true;
286 (%i2) cons(a,-a), inflag : false;
291 @opencatbox{Categories:}
293 @category{Expressions}
297 @c -----------------------------------------------------------------------------
299 @deffn {Function} copylist (@var{list})
301 Returns a copy of the list @var{list}.
303 @opencatbox{Categories:}
308 @c -----------------------------------------------------------------------------
310 @deffn {Function} create_list (@var{form}, @var{x_1}, @var{list_1}, @dots{}, @var{x_n}, @var{list_n})
312 Create a list by evaluating @var{form} with @var{x_1} bound to
313 each element of @var{list_1}, and for each such binding bind @var{x_2}
314 to each element of @var{list_2}, @dots{}
315 The number of elements in the result will be
316 the product of the number of elements in each list.
317 Each variable @var{x_i} must actually be a symbol -- it will not be evaluated.
318 The list arguments will be evaluated once at the beginning of the
322 @c create_list (x^i, i, [1, 3, 7]);
326 (%i1) create_list (x^i, i, [1, 3, 7]);
333 With a double iteration:
336 @c create_list ([i, j], i, [a, b], j, [e, f, h]);
340 (%i1) create_list ([i, j], i, [a, b], j, [e, f, h]);
341 (%o1) [[a, e], [a, f], [a, h], [b, e], [b, f], [b, h]]
345 Instead of @var{list_i} two args may be supplied each of which should
346 evaluate to a number. These will be the inclusive lower and
347 upper bounds for the iteration.
350 @c create_list ([i, j], i, [1, 2, 3], j, 1, i);
354 (%i1) create_list ([i, j], i, [1, 2, 3], j, 1, i);
355 (%o1) [[1, 1], [2, 1], [2, 2], [3, 1], [3, 2], [3, 3]]
359 Note that the limits or list for the @code{j} variable can
360 depend on the current value of @code{i}.
362 @opencatbox{Categories:}
367 @c -----------------------------------------------------------------------------
369 @deffn {Function} delete @
370 @fname{delete} (@var{expr_1}, @var{expr_2}) @
371 @fname{delete} (@var{expr_1}, @var{expr_2}, @var{n})
373 @code{delete(@var{expr_1}, @var{expr_2})}
374 removes from @var{expr_2} any arguments of its top-level operator
375 which are the same (as determined by "=") as @var{expr_1}.
376 Note that "=" tests for formal equality, not equivalence.
377 Note also that arguments of subexpressions are not affected.
379 @var{expr_1} may be an atom or a non-atomic expression.
380 @var{expr_2} may be any non-atomic expression.
381 @code{delete} returns a new expression;
382 it does not modify @var{expr_2}.
384 @code{delete(@var{expr_1}, @var{expr_2}, @var{n})}
385 removes from @var{expr_2} the first @var{n} arguments of the top-level operator
386 which are the same as @var{expr_1}.
387 If there are fewer than @var{n} such arguments,
388 then all such arguments are removed.
392 Removing elements from a list.
395 @c delete (y, [w, x, y, z, z, y, x, w]);
399 (%i1) delete (y, [w, x, y, z, z, y, x, w]);
400 (%o1) [w, x, z, z, x, w]
404 Removing terms from a sum.
407 @c delete (sin(x), x + sin(x) + y);
411 (%i1) delete (sin(x), x + sin(x) + y);
416 Removing factors from a product.
419 @c delete (u - x, (u - w)*(u - x)*(u - y)*(u - z));
423 (%i1) delete (u - x, (u - w)*(u - x)*(u - y)*(u - z));
424 (%o1) (u - w) (u - y) (u - z)
428 Removing arguments from an arbitrary expression.
431 @c delete (a, foo (a, b, c, d, a));
435 (%i1) delete (a, foo (a, b, c, d, a));
440 Limit the number of removed arguments.
443 @c delete (a, foo (a, b, a, c, d, a), 2);
447 (%i1) delete (a, foo (a, b, a, c, d, a), 2);
448 (%o1) foo(b, c, d, a)
452 Whether arguments are the same as @var{expr_1} is determined by "=".
453 Arguments which are @code{equal} but not "=" are not removed.
456 @c [is (equal (0, 0)), is (equal (0, 0.0)), is (equal (0, 0b0))];
457 @c [is (0 = 0), is (0 = 0.0), is (0 = 0b0)];
458 @c delete (0, [0, 0.0, 0b0]);
459 @c is (equal ((x + y)*(x - y), x^2 - y^2));
460 @c is ((x + y)*(x - y) = x^2 - y^2);
461 @c delete ((x + y)*(x - y), [(x + y)*(x - y), x^2 - y^2]);
465 (%i1) [is (equal (0, 0)), is (equal (0, 0.0)), is (equal (0, 0b0))];
466 (%o1) [true, true, true]
469 (%i2) [is (0 = 0), is (0 = 0.0), is (0 = 0b0)];
470 (%o2) [true, false, false]
473 (%i3) delete (0, [0, 0.0, 0b0]);
477 (%i4) is (equal ((x + y)*(x - y), x^2 - y^2));
481 (%i5) is ((x + y)*(x - y) = x^2 - y^2);
485 (%i6) delete ((x + y)*(x - y), [(x + y)*(x - y), x^2 - y^2]);
491 @opencatbox{Categories:}
493 @category{Expressions}
497 @c -----------------------------------------------------------------------------
499 @deffn {Function} eighth (@var{expr})
501 Returns the 8th item of expression or list @var{expr}.
502 See @mref{first} for more details.
504 @opencatbox{Categories:}
506 @category{Expressions}
511 @c -----------------------------------------------------------------------------
513 @deffn {Function} endcons @
514 @fname{endcons} (@var{expr}, @var{list}) @
515 @fname{endcons} (@var{expr_1}, @var{expr_2})
517 @code{endcons (@var{expr}, @var{list})} returns a new list constructed of the elements of
518 @var{list} followed by @var{expr}. The Maxima function @code{endcons} can also be used where
519 the second argument is other than a list and this might be useful. In this case,
520 @code{endcons (@var{expr_1}, @var{expr_2})} returns an expression with same operator as
521 @var{expr_2} but with argument @code{endcons(expr_1, args(expr_2))}. Examples:
524 @c endcons(a,[b,c,d]);
525 @c endcons(a,f(b,c,d));
529 (%i1) endcons(a,[b,c,d]);
533 (%i2) endcons(a,f(b,c,d));
538 In general, @code{endcons} applied to a nonlist doesn't make sense. For instance, @code{endcons(a,b^c)}
539 results in an illegal expression, since '^' cannot take three arguments.
541 When @code{inflag} is true, @code{endcons} operates on the internal structure of an expression, otherwise
542 @code{endcons} operates on the displayed form. Especially when @code{inflag} is true, @code{endcons} applied
543 to a nonlist sometimes gives a surprising result; for example
546 @c endcons(a,-a), inflag : true;
547 @c endcons(a,-a), inflag : false;
551 (%i1) endcons(a,-a), inflag : true;
556 (%i2) endcons(a,-a), inflag : false;
561 @opencatbox{Categories:}
563 @category{Expressions}
568 @c -----------------------------------------------------------------------------
570 @deffn {Function} fifth (@var{expr})
572 Returns the 5th item of expression or list @var{expr}.
573 See @mref{first} for more details.
575 @opencatbox{Categories:}
577 @category{Expressions}
581 @c -----------------------------------------------------------------------------
583 @deffn {Function} first (@var{expr})
585 Returns the first part of @var{expr} which may result in the first element of a
586 list, the first row of a matrix, the first term of a sum, etc.:
590 (%i1) matrix([1,2],[3,4]);
598 (%i4) first(a*b/c+d+e/x);
602 (%i5) first(a=b/c+d+e/x);
608 @code{first} and its related functions, @code{rest} and @code{last}, work
609 on the form of @var{expr} which is displayed not the form which is typed on
610 input. If the variable @mref{inflag} is set to @code{true} however, these
611 functions will look at the internal form of @var{expr}. One reason why this may
612 make a difference is that the simplifier re-orders expressions:
618 (%i2) first(x+y),inflag : true;
620 (%i3) first(x+y),inflag : false;
625 The functions @code{second} @dots{}
626 @code{tenth} yield the second through the tenth part of their input argument.
628 See also @mref{firstn} and @mrefdot{part}
630 @opencatbox{Categories:}
632 @category{Expressions}
636 @c -----------------------------------------------------------------------------
638 @deffn {Function} firstn (@var{expr}, @var{count})
640 Returns the first @var{count} arguments of @var{expr}, if @var{expr} has at least @var{count} arguments.
641 Returns @var{expr} if @var{expr} has less than @var{count} arguments.
643 @var{expr} may be any nonatomic expression.
644 When @var{expr} is something other than a list,
645 @code{firstn} returns an expression which has the same operator as @var{expr}.
646 @var{count} must be a nonnegative integer.
648 @code{firstn} honors the global flag @code{inflag},
649 which governs whether the internal form of an expression is processed (when @code{inflag} is true)
650 or the displayed form (when @code{inflag} is false).
652 Note that @code{firstn(@var{expr}, 1)},
653 which returns a nonatomic expression containing the first argument,
654 is not the same as @code{first(@var{expr})},
655 which returns the first argument by itself.
657 See also @mref{lastn} and @mrefdot{rest}
661 @code{firstn} returns the first @var{count} elements of @var{expr}, if @var{expr} has at least @var{count} elements.
664 @c mylist : [1, a, 2, b, 3, x, 4 - y, 2*z + sin(u)];
665 @c firstn (mylist, 0);
666 @c firstn (mylist, 1);
667 @c firstn (mylist, 2);
668 @c firstn (mylist, 7);
672 (%i1) mylist : [1, a, 2, b, 3, x, 4 - y, 2*z + sin(u)];
673 (%o1) [1, a, 2, b, 3, x, 4 - y, 2 z + sin(u)]
676 (%i2) firstn (mylist, 0);
680 (%i3) firstn (mylist, 1);
684 (%i4) firstn (mylist, 2);
688 (%i5) firstn (mylist, 7);
689 (%o5) [1, a, 2, b, 3, x, 4 - y]
693 @code{firstn} returns @var{expr} if @var{expr} has less than @var{count} elements.
696 @c mylist : [1, a, 2, b, 3, x, 4 - y, 2*z + sin(u)];
697 @c firstn (mylist, 100);
701 (%i1) mylist : [1, a, 2, b, 3, x, 4 - y, 2*z + sin(u)];
702 (%o1) [1, a, 2, b, 3, x, 4 - y, 2 z + sin(u)]
705 (%i2) firstn (mylist, 100);
706 (%o2) [1, a, 2, b, 3, x, 4 - y, 2 z + sin(u)]
710 @var{expr} may be any nonatomic expression.
713 @c myfoo : foo(1, a, 2, b, 3, x, 4 - y, 2*z + sin(u));
714 @c firstn (myfoo, 4);
715 @c mybar : bar[m, n](1, a, 2, b, 3, x, 4 - y, 2*z + sin(u));
716 @c firstn (mybar, 4);
717 @c mymatrix : genmatrix (lambda ([i, j], 10*i + j), 10, 4) $
718 @c firstn (mymatrix, 3);
722 (%i1) myfoo : foo(1, a, 2, b, 3, x, 4 - y, 2*z + sin(u));
723 (%o1) foo(1, a, 2, b, 3, x, 4 - y, 2 z + sin(u))
726 (%i2) firstn (myfoo, 4);
727 (%o2) foo(1, a, 2, b)
730 (%i3) mybar : bar[m, n](1, a, 2, b, 3, x, 4 - y, 2*z + sin(u));
731 (%o3) bar (1, a, 2, b, 3, x, 4 - y, 2 z + sin(u))
735 (%i4) firstn (mybar, 4);
736 (%o4) bar (1, a, 2, b)
739 (%i5) mymatrix : genmatrix (lambda ([i, j], 10*i + j), 10, 4) $
741 (%i6) firstn (mymatrix, 3);
744 (%o6) [ 21 22 23 24 ]
750 @code{firstn} honors the global flag @code{inflag}.
753 @c myexpr : a + b + c + d + e;
754 @c firstn (myexpr, 3), inflag=true;
755 @c firstn (myexpr, 3), inflag=false;
759 (%i1) myexpr : a + b + c + d + e;
760 (%o1) e + d + c + b + a
763 (%i2) firstn (myexpr, 3), inflag=true;
767 (%i3) firstn (myexpr, 3), inflag=false;
772 Note that @code{firstn(@var{expr}, 1)} is not the same as @code{first(@var{expr})}.
775 @c firstn ([w, x, y, z], 1);
776 @c first ([w, x, y, z]);
780 (%i1) firstn ([w, x, y, z], 1);
784 (%i2) first ([w, x, y, z]);
789 @opencatbox{Categories:}
791 @category{Expressions}
795 @c -----------------------------------------------------------------------------
797 @deffn {Function} fourth (@var{expr})
799 Returns the 4th item of expression or list @var{expr}.
800 See @mref{first} for more details.
802 @opencatbox{Categories:}
804 @category{Expressions}
808 @c -----------------------------------------------------------------------------
810 @deffn {Function} join (@var{l}, @var{m})
812 Creates a new list containing the elements of lists @var{l} and @var{m},
813 interspersed. The result has elements @code{[@var{l}[1], @var{m}[1],
814 @var{l}[2], @var{m}[2], ...]}. The lists @var{l} and @var{m} may contain any
817 If the lists are different lengths, @code{join} ignores elements of the longer
820 Maxima complains if @var{l} or @var{m} is not a list.
822 See also @mrefdot{append}
827 @c L1: [a, sin(b), c!, d - 1];
828 @c join (L1, [1, 2, 3, 4]);
829 @c join (L1, [aa, bb, cc, dd, ee, ff]);
833 (%i1) L1: [a, sin(b), c!, d - 1];
834 (%o1) [a, sin(b), c!, d - 1]
837 (%i2) join (L1, [1, 2, 3, 4]);
838 (%o2) [a, 1, sin(b), 2, c!, 3, d - 1, 4]
841 (%i3) join (L1, [aa, bb, cc, dd, ee, ff]);
842 (%o3) [a, aa, sin(b), bb, c!, cc, d - 1, dd]
846 @opencatbox{Categories:}
852 @c HOW IS "LAST" PART DETERMINED ??
854 @c -----------------------------------------------------------------------------
856 @deffn {Function} last (@var{expr})
858 Returns the last part (term, row, element, etc.) of the @var{expr}.
860 See also @mrefdot{lastn}
862 @opencatbox{Categories:}
864 @category{Expressions}
868 @c NEEDS CLARIFICATION AND EXAMPLES
870 @c -----------------------------------------------------------------------------
872 @deffn {Function} lastn (@var{expr}, @var{count})
874 Returns the last @var{count} arguments of @var{expr}, if @var{expr} has at least @var{count} arguments.
875 Returns @var{expr} if @var{expr} has less than @var{count} arguments.
877 @var{expr} may be any nonatomic expression.
878 When @var{expr} is something other than a list,
879 @code{lastn} returns an expression which has the same operator as @var{expr}.
880 @var{count} must be a nonnegative integer.
882 @code{lastn} honors the global flag @code{inflag},
883 which governs whether the internal form of an expression is processed (when @code{inflag} is true)
884 or the displayed form (when @code{inflag} is false).
886 Note that @code{lastn(@var{expr}, 1)},
887 which returns a nonatomic expression containing the last argument,
888 is not the same as @code{last(@var{expr})},
889 which returns the last argument by itself.
891 See also @mref{firstn} and @mrefdot{rest}
895 @code{lastn} returns the last @var{count} elements of @var{expr}, if @var{expr} has at least @var{count} elements.
898 @c mylist : [1, a, 2, b, 3, x, 4 - y, 2*z + sin(u)];
899 @c lastn (mylist, 0);
900 @c lastn (mylist, 1);
901 @c lastn (mylist, 2);
902 @c lastn (mylist, 7);
906 (%i1) mylist : [1, a, 2, b, 3, x, 4 - y, 2*z + sin(u)];
907 (%o1) [1, a, 2, b, 3, x, 4 - y, 2 z + sin(u)]
910 (%i2) lastn (mylist, 0);
914 (%i3) lastn (mylist, 1);
918 (%i4) lastn (mylist, 2);
919 (%o4) [4 - y, 2 z + sin(u)]
922 (%i5) lastn (mylist, 7);
923 (%o5) [a, 2, b, 3, x, 4 - y, 2 z + sin(u)]
927 @code{lastn} returns @var{expr} if @var{expr} has less than @var{count} elements.
930 @c mylist : [1, a, 2, b, 3, x, 4 - y, 2*z + sin(u)];
931 @c lastn (mylist, 100);
935 (%i1) mylist : [1, a, 2, b, 3, x, 4 - y, 2*z + sin(u)];
936 (%o1) [1, a, 2, b, 3, x, 4 - y, 2 z + sin(u)]
939 (%i2) lastn (mylist, 100);
940 (%o2) [1, a, 2, b, 3, x, 4 - y, 2 z + sin(u)]
944 @var{expr} may be any nonatomic expression.
947 @c myfoo : foo(1, a, 2, b, 3, x, 4 - y, 2*z + sin(u));
949 @c mybar : bar[m, n](1, a, 2, b, 3, x, 4 - y, 2*z + sin(u));
951 @c mymatrix : genmatrix (lambda ([i, j], 10*i + j), 10, 4) $
952 @c lastn (mymatrix, 3);
956 (%i1) myfoo : foo(1, a, 2, b, 3, x, 4 - y, 2*z + sin(u));
957 (%o1) foo(1, a, 2, b, 3, x, 4 - y, 2 z + sin(u))
960 (%i2) lastn (myfoo, 4);
961 (%o2) foo(3, x, 4 - y, 2 z + sin(u))
964 (%i3) mybar : bar[m, n](1, a, 2, b, 3, x, 4 - y, 2*z + sin(u));
965 (%o3) bar (1, a, 2, b, 3, x, 4 - y, 2 z + sin(u))
969 (%i4) lastn (mybar, 4);
970 (%o4) bar (3, x, 4 - y, 2 z + sin(u))
973 (%i5) mymatrix : genmatrix (lambda ([i, j], 10*i + j), 10, 4) $
975 (%i6) lastn (mymatrix, 3);
978 (%o6) [ 91 92 93 94 ]
984 @code{lastn} honors the global flag @code{inflag}.
987 @c myexpr : a + b + c + d + e;
988 @c lastn (myexpr, 3), inflag=true;
989 @c lastn (myexpr, 3), inflag=false;
993 (%i1) myexpr : a + b + c + d + e;
994 (%o1) e + d + c + b + a
997 (%i2) lastn (myexpr, 3), inflag=true;
1001 (%i3) lastn (myexpr, 3), inflag=false;
1006 Note that @code{lastn(@var{expr}, 1)} is not the same as @code{last(@var{expr})}.
1009 @c lastn ([w, x, y, z], 1);
1010 @c last ([w, x, y, z]);
1014 (%i1) lastn ([w, x, y, z], 1);
1018 (%i2) last ([w, x, y, z]);
1023 @opencatbox{Categories:}
1025 @category{Expressions}
1029 @c -----------------------------------------------------------------------------
1031 @deffn {Function} length (@var{expr})
1033 Returns (by default) the number of parts in the external
1034 (displayed) form of @var{expr}. For lists this is the number of elements,
1035 for matrices it is the number of rows, and for sums it is the number
1036 of terms (see @mref{dispform}).
1038 The @code{length} command is affected by the @mref{inflag} switch. So, e.g.
1039 @code{length(a/(b*c));} gives 2 if @code{inflag} is @code{false} (Assuming
1040 @mref{exptdispflag} is @code{true}), but 3 if @code{inflag} is @code{true} (the
1041 internal representation is essentially @code{a*b^-1*c^-1}).
1043 Determining a list's length typically needs an amount of time proportional
1044 to the number of elements in the list. If the length of a list is used inside
1045 a loop it therefore might drastically increase the performance if the length
1046 is calculated outside the loop instead.
1048 @opencatbox{Categories:}
1050 @category{Expressions}
1054 @c -----------------------------------------------------------------------------
1056 @defvr {Option variable} listarith
1057 Default value: @code{true}
1059 If @code{false} causes any arithmetic operations with lists to be suppressed;
1060 when @code{true}, list-matrix operations are contagious causing lists to be
1061 converted to matrices yielding a result which is always a matrix. However,
1062 list-list operations should return lists.
1064 @opencatbox{Categories:}
1066 @category{Global flags}
1070 @c -----------------------------------------------------------------------------
1071 @deffn {Function} listp (@var{expr})
1073 Returns @code{true} if @var{expr} is a list else @code{false}.
1075 @opencatbox{Categories:}
1077 @category{Predicate functions}
1081 @c -----------------------------------------------------------------------------
1083 @deffn {Function} lreduce @
1084 @fname{lreduce} (@var{F}, @var{s}) @
1085 @fname{lreduce} (@var{F}, @var{s}, @var{s_0})
1087 Extends the binary function @var{F} to an n-ary function by composition,
1088 where @var{s} is a list.
1090 @code{lreduce(@var{F}, @var{s})} returns @code{F(... F(F(s_1, s_2), s_3), ... s_n)}.
1091 When the optional argument @var{s_0} is present,
1092 the result is equivalent to @code{lreduce(@var{F}, cons(@var{s_0}, @var{s}))}.
1094 The function @var{F} is first applied to the
1095 @i{leftmost} list elements, thus the name "lreduce".
1097 See also @mref{rreduce}, @mref{xreduce}, and @mref{tree_reduce}.
1101 @code{lreduce} without the optional argument.
1104 @c lreduce (f, [1, 2, 3]);
1105 @c lreduce (f, [1, 2, 3, 4]);
1109 (%i1) lreduce (f, [1, 2, 3]);
1113 (%i2) lreduce (f, [1, 2, 3, 4]);
1114 (%o2) f(f(f(1, 2), 3), 4)
1118 @code{lreduce} with the optional argument.
1121 @c lreduce (f, [1, 2, 3], 4);
1125 (%i1) lreduce (f, [1, 2, 3], 4);
1126 (%o1) f(f(f(4, 1), 2), 3)
1130 @code{lreduce} applied to built-in binary operators.
1131 @code{/} is the division operator.
1134 @c lreduce ("^", args ({a, b, c, d}));
1135 @c lreduce ("/", args ({a, b, c, d}));
1139 (%i1) lreduce ("^", args (@{a, b, c, d@}));
1144 (%i2) lreduce ("/", args (@{a, b, c, d@}));
1151 @opencatbox{Categories:}
1157 @c -----------------------------------------------------------------------------
1159 @deffn {Function} makelist @
1160 @fname{makelist} () @
1161 @fname{makelist} (@var{expr}, @var{n}) @
1162 @fname{makelist} (@var{expr}, @var{i}, @var{i_max}) @
1163 @fname{makelist} (@var{expr}, @var{i}, @var{i_0}, @var{i_max}) @
1164 @fname{makelist} (@var{expr}, @var{i}, @var{i_0}, @var{i_max}, @var{step}) @
1165 @fname{makelist} (@var{expr}, @var{x}, @var{list})
1167 The first form, @code{makelist ()}, creates an empty list. The second form,
1168 @code{makelist (@var{expr})}, creates a list with @var{expr} as its single
1169 element. @code{makelist (@var{expr}, @var{n})} creates a list of @var{n}
1170 elements generated from @var{expr}.
1172 The most general form, @code{makelist (@var{expr}, @var{i}, @var{i_0},
1173 @var{i_max}, @var{step})}, returns the list of elements obtained when
1174 @code{ev (@var{expr}, @var{i}=@var{j})} is applied to the elements
1175 @var{j} of the sequence: @var{i_0}, @var{i_0} + @var{step}, @var{i_0} +
1176 2*@var{step}, ..., with @var{|j|} less than or equal to @var{|i_max|}.
1178 The increment @var{step} can be a number (positive or negative) or an
1179 expression. If it is omitted, the default value 1 will be used. If both
1180 @var{i_0} and @var{step} are omitted, they will both have a default
1183 @code{makelist (@var{expr}, @var{x}, @var{list})} returns a list, the
1184 @code{j}th element of which is equal to
1185 @code{ev (@var{expr}, @var{x}=@var{list}[j])} for @code{j} equal to 1 through
1186 @code{length (@var{list})}.
1191 @c makelist (concat (x,i), i, 6);
1192 @c makelist (x=y, y, [a, b, c]);
1193 @c makelist (x^2, x, 3, 2*%pi, 2);
1194 @c makelist (random(6), 4);
1195 @c flatten (makelist (makelist (i^2, 3), i, 4));
1196 @c flatten (makelist (makelist (i^2, i, 3), 4));
1200 (%i1) makelist (concat (x,i), i, 6);
1201 (%o1) [x1, x2, x3, x4, x5, x6]
1204 (%i2) makelist (x=y, y, [a, b, c]);
1205 (%o2) [x = a, x = b, x = c]
1208 (%i3) makelist (x^2, x, 3, 2*%pi, 2);
1212 (%i4) makelist (random(6), 4);
1216 (%i5) flatten (makelist (makelist (i^2, 3), i, 4));
1217 (%o5) [1, 1, 1, 4, 4, 4, 9, 9, 9, 16, 16, 16]
1220 (%i6) flatten (makelist (makelist (i^2, i, 3), 4));
1221 (%o6) [1, 4, 9, 1, 4, 9, 1, 4, 9, 1, 4, 9]
1225 @opencatbox{Categories:}
1230 @c -----------------------------------------------------------------------------
1232 @deffn {Function} member (@var{expr_1}, @var{expr_2})
1234 Returns @code{true} if @code{is(@var{expr_1} = @var{a})}
1235 for some element @var{a} in @code{args(@var{expr_2})},
1236 otherwise returns @code{false}.
1238 @code{expr_2} is typically a list, in which case
1239 @code{args(@var{expr_2}) = @var{expr_2}} and @code{is(@var{expr_1} = @var{a})}
1240 for some element @var{a} in @code{expr_2} is the test.
1242 @code{member} does not inspect parts of the arguments of @code{expr_2}, so it
1243 may return @code{false} even if @code{expr_1} is a part of some argument of
1246 See also @mrefdot{elementp}
1251 @c member (8, [8, 8.0, 8b0]);
1252 @c member (8, [8.0, 8b0]);
1253 @c member (b, [a, b, c]);
1254 @c member (b, [[a, b], [b, c]]);
1255 @c member ([b, c], [[a, b], [b, c]]);
1256 @c F (1, 1/2, 1/4, 1/8);
1258 @c member ("ab", ["aa", "ab", sin(1), a + b]);
1262 (%i1) member (8, [8, 8.0, 8b0]);
1266 (%i2) member (8, [8.0, 8b0]);
1270 (%i3) member (b, [a, b, c]);
1274 (%i4) member (b, [[a, b], [b, c]]);
1278 (%i5) member ([b, c], [[a, b], [b, c]]);
1282 (%i6) F (1, 1/2, 1/4, 1/8);
1288 (%i7) member (1/8, %);
1292 (%i8) member ("ab", ["aa", "ab", sin(1), a + b]);
1297 @opencatbox{Categories:}
1299 @category{Expressions}
1300 @category{Predicate functions}
1304 @c -----------------------------------------------------------------------------
1306 @deffn {Function} ninth (@var{expr})
1308 Returns the 9th item of expression or list @var{expr}.
1309 See @mref{first} for more details.
1311 @opencatbox{Categories:}
1313 @category{Expressions}
1317 @c -----------------------------------------------------------------------------
1319 @deffn {Function} pop (@var{list})
1321 @code{pop} removes and returns the first element from the list @var{list}. The argument
1322 @var{list} must be a mapatom that is bound to a nonempty list. If the argument @var{list} is
1323 not bound to a nonempty list, Maxima signals an error. For examples, see @mrefdot{push}
1325 @opencatbox{Categories:}
1327 @category{Expressions}
1331 @c -----------------------------------------------------------------------------
1333 @deffn {Function} push (@var{item}, @var{list})
1335 @code{push} prepends the item @var{item} to the list @var{list} and returns a copy of the new list.
1336 The second argument @var{list} must be a mapatom that is bound to a list. The first argument @var{item}
1337 can be any Maxima symbol or expression. If the argument @var{list} is not bound to a list, Maxima
1341 To remove the first item from a list, see @mrefdot{pop}
1349 @c push (x^2+y, ll);
1350 @c a: push ("string", ll);
1367 (%i3) push (x^2+y, ll);
1372 (%i4) a: push ("string", ll);
1374 (%o4) [string, y + x , x]
1396 (%o9) [string, y + x , x]
1400 @opencatbox{Categories:}
1402 @category{Expressions}
1406 @c -----------------------------------------------------------------------------
1408 @deffn {Function} rest @
1409 @fname{rest} (@var{expr}, @var{n}) @
1410 @fname{rest} (@var{expr})
1412 Returns @var{expr} with its first @var{n} elements removed if @var{n}
1413 is positive and its last @code{- @var{n}} elements removed if @var{n}
1414 is negative. If @var{n} is 1 it may be omitted. The first argument
1415 @var{expr} may be a list, matrix, or other expression. When @var{expr}
1416 is an atom, @code{rest} signals an error; when @var{expr} is an empty
1417 list and @code{partswitch} is false, @code{rest} signals an error. When
1418 @var{expr} is an empty list and @code{partswitch} is true, @code{rest}
1421 Applying @code{rest} to expression such as @code{f(a,b,c)} returns
1422 @code{f(b,c)}. In general, applying @code{rest} to a nonlist doesn't
1423 make sense. For example, because '^' requires two arguments,
1424 @code{rest(a^b)} results in an error message. The functions
1425 @code{args} and @code{op} may be useful as well, since @code{args(a^b)}
1426 returns @code{[a,b]} and @code{op(a^b)} returns ^.
1428 See also @mref{firstn} and @mrefdot{lastn}
1434 (%i2) rest(a+b+c,2);
1436 (%i3) rest(a+b+c,-2);
1441 @opencatbox{Categories:}
1443 @category{Expressions}
1447 @c NEED ANOTHER deffn FOR GENERAL EXPRESSIONS ARGUMENTS
1448 @c SPLIT OFF EXAMPLES INTO EXAMPLE SECTION
1450 @c -----------------------------------------------------------------------------
1452 @deffn {Function} reverse (@var{list})
1454 Reverses the order of the members of the @var{list} (not
1455 the members themselves). @code{reverse} also works on general expressions,
1456 e.g. @code{reverse(a=b);} gives @code{b=a}.
1458 See also @mrefdot{sreverse}
1460 @opencatbox{Categories:}
1462 @category{Expressions}
1466 @c -----------------------------------------------------------------------------
1468 @deffn {Function} rreduce @
1469 @fname{rreduce} (@var{F}, @var{s}) @
1470 @fname{rreduce} (@var{F}, @var{s}, @var{s_@{n + 1@}})
1472 Extends the binary function @var{F} to an n-ary function by composition,
1473 where @var{s} is a list.
1475 @code{rreduce(@var{F}, @var{s})} returns @code{F(s_1, ... F(s_@{n - 2@}, F(s_@{n - 1@}, s_n)))}.
1476 When the optional argument @var{s_@{n + 1@}} is present,
1477 the result is equivalent to @code{rreduce(@var{F}, endcons(@var{s_@{n + 1@}}, @var{s}))}.
1479 The function @var{F} is first applied to the
1480 @i{rightmost} list elements, thus the name "rreduce".
1482 See also @mref{lreduce}, @mref{tree_reduce}, and @mref{xreduce}.
1486 @code{rreduce} without the optional argument.
1489 @c rreduce (f, [1, 2, 3]);
1490 @c rreduce (f, [1, 2, 3, 4]);
1494 (%i1) rreduce (f, [1, 2, 3]);
1498 (%i2) rreduce (f, [1, 2, 3, 4]);
1499 (%o2) f(1, f(2, f(3, 4)))
1503 @code{rreduce} with the optional argument.
1506 @c rreduce (f, [1, 2, 3], 4);
1510 (%i1) rreduce (f, [1, 2, 3], 4);
1511 (%o1) f(1, f(2, f(3, 4)))
1515 @code{rreduce} applied to built-in binary operators.
1516 @code{/} is the division operator.
1519 @c rreduce ("^", args ({a, b, c, d}));
1520 @c rreduce ("/", args ({a, b, c, d}));
1524 (%i1) rreduce ("^", args (@{a, b, c, d@}));
1531 (%i2) rreduce ("/", args (@{a, b, c, d@}));
1538 @opencatbox{Categories:}
1544 @c -----------------------------------------------------------------------------
1546 @deffn {Function} second (@var{expr})
1548 Returns the 2nd item of expression or list @var{expr}.
1549 See @mref{first} for more details.
1551 @opencatbox{Categories:}
1553 @category{Expressions}
1557 @c -----------------------------------------------------------------------------
1559 @deffn {Function} seventh (@var{expr})
1561 Returns the 7th item of expression or list @var{expr}.
1562 See @mref{first} for more details.
1564 @opencatbox{Categories:}
1566 @category{Expressions}
1570 @c -----------------------------------------------------------------------------
1572 @deffn {Function} sixth (@var{expr})
1574 Returns the 6th item of expression or list @var{expr}.
1575 See @mref{first} for more details.
1577 @opencatbox{Categories:}
1579 @category{Expressions}
1583 @c -----------------------------------------------------------------------------
1585 @deffn {Function} sort @
1586 @fname{sort} (@var{L}, @var{P}) @
1587 @fname{sort} (@var{L})
1589 @code{sort(@var{L}, @var{P})} sorts a list @var{L} according to a predicate @code{P} of two arguments
1590 which defines a strict weak order on the elements of @var{L}.
1591 If @code{@var{P}(a, b)} is @code{true}, then @code{a} appears before @code{b} in the result.
1592 If neither @code{@var{P}(a, b)} nor @code{@var{P}(b, a)} are @code{true},
1593 then @code{a} and @code{b} are equivalent, and appear in the result in the same order as in the input.
1594 That is, @code{sort} is a stable sort.
1596 If @code{@var{P}(a, b)} and @code{@var{P}(b, a)} are both @code{true} for some elements of @var{L},
1597 then @var{P} is not a valid sort predicate, and the result is undefined.
1598 If @code{@var{P}(a, b)} is something other than @code{true} or @code{false}, @code{sort} signals an error.
1600 The predicate may be specified as the name of a function
1601 or binary infix operator, or as a @code{lambda} expression. If specified as
1602 the name of an operator, the name must be enclosed in double quotes.
1604 The sorted list is returned as a new object; the argument @var{L} is not modified.
1606 @code{sort(@var{L})} is equivalent to @code{sort(@var{L}, orderlessp)}.
1608 The default sorting order is ascending, as determined by @mrefdot{orderlessp} The predicate @code{ordergreatp} sorts a list in descending order.
1610 All Maxima atoms and expressions are comparable under @code{orderlessp} and @code{ordergreatp}.
1612 Operators @code{<} and @code{>} order numbers, constants, and constant expressions by magnitude.
1613 Note that @code{orderlessp} and @code{ordergreatp} do not order numbers, constants, and constant expressions by magnitude.
1615 @code{ordermagnitudep} orders numbers, constants, and constant expressions the same as @code{<},
1616 and all other elements the same as @code{orderlessp}.
1620 @code{sort} sorts a list according to a predicate of two arguments
1621 which defines a strict weak order on the elements of the list.
1624 @c sort ([1, a, b, 2, 3, c], 'orderlessp);
1625 @c sort ([1, a, b, 2, 3, c], 'ordergreatp);
1629 (%i1) sort ([1, a, b, 2, 3, c], 'orderlessp);
1630 (%o1) [1, 2, 3, a, b, c]
1633 (%i2) sort ([1, a, b, 2, 3, c], 'ordergreatp);
1634 (%o2) [c, b, a, 3, 2, 1]
1638 The predicate may be specified as the name of a function
1639 or binary infix operator, or as a @code{lambda} expression. If specified as
1640 the name of an operator, the name must be enclosed in double quotes.
1643 @c L : [[1, x], [3, y], [4, w], [2, z]];
1644 @c foo (a, b) := a[1] > b[1];
1647 @c a >> b := a[1] > b[1];
1649 @c sort (L, lambda ([a, b], a[1] > b[1]));
1653 (%i1) L : [[1, x], [3, y], [4, w], [2, z]];
1654 (%o1) [[1, x], [3, y], [4, w], [2, z]]
1657 (%i2) foo (a, b) := a[1] > b[1];
1658 (%o2) foo(a, b) := a > b
1662 (%i3) sort (L, 'foo);
1663 (%o3) [[4, w], [3, y], [2, z], [1, x]]
1670 (%i5) a >> b := a[1] > b[1];
1671 (%o5) (a >> b) := a > b
1675 (%i6) sort (L, ">>");
1676 (%o6) [[4, w], [3, y], [2, z], [1, x]]
1679 (%i7) sort (L, lambda ([a, b], a[1] > b[1]));
1680 (%o7) [[4, w], [3, y], [2, z], [1, x]]
1684 @code{sort(@var{L})} is equivalent to @code{sort(@var{L}, orderlessp)}.
1687 @c L : [a, 2*b, -5, 7, 1 + %e, %pi];
1689 @c sort (L, 'orderlessp);
1693 (%i1) L : [a, 2*b, -5, 7, 1 + %e, %pi];
1694 (%o1) [a, 2 b, - 5, 7, %e + 1, %pi]
1698 (%o2) [- 5, 7, %e + 1, %pi, a, 2 b]
1701 (%i3) sort (L, 'orderlessp);
1702 (%o3) [- 5, 7, %e + 1, %pi, a, 2 b]
1706 The default sorting order is ascending, as determined by @mrefdot{orderlessp} The predicate @code{ordergreatp} sorts a list in descending order.
1709 @c L : [a, 2*b, -5, 7, 1 + %e, %pi];
1711 @c sort (L, 'ordergreatp);
1715 (%i1) L : [a, 2*b, -5, 7, 1 + %e, %pi];
1716 (%o1) [a, 2 b, - 5, 7, %e + 1, %pi]
1720 (%o2) [- 5, 7, %e + 1, %pi, a, 2 b]
1723 (%i3) sort (L, 'ordergreatp);
1724 (%o3) [2 b, a, %pi, %e + 1, 7, - 5]
1728 All Maxima atoms and expressions are comparable under @code{orderlessp} and @code{ordergreatp}.
1731 @c L : [11, -17, 29b0, 9*c, 7.55, foo(x, y), -5/2, b + a];
1732 @c sort (L, orderlessp);
1733 @c sort (L, ordergreatp);
1737 (%i1) L : [11, -17, 29b0, 9*c, 7.55, foo(x, y), -5/2, b + a];
1739 (%o1) [11, - 17, 2.9b1, 9 c, 7.55, foo(x, y), - -, b + a]
1743 (%i2) sort (L, orderlessp);
1745 (%o2) [- 17, - -, 7.55, 11, 2.9b1, b + a, 9 c, foo(x, y)]
1749 (%i3) sort (L, ordergreatp);
1751 (%o3) [foo(x, y), 9 c, b + a, 2.9b1, 11, 7.55, - -, - 17]
1756 Operators @code{<} and @code{>} order numbers, constants, and constant expressions by magnitude.
1757 Note that @code{orderlessp} and @code{ordergreatp} do not order numbers, constants, and constant expressions by magnitude.
1760 @c L : [%pi, 3, 4, %e, %gamma];
1762 @c sort (L, ordergreatp);
1766 (%i1) L : [%pi, 3, 4, %e, %gamma];
1767 (%o1) [%pi, 3, 4, %e, %gamma]
1770 (%i2) sort (L, ">");
1771 (%o2) [4, %pi, 3, %e, %gamma]
1774 (%i3) sort (L, ordergreatp);
1775 (%o3) [%pi, %gamma, %e, 4, 3]
1779 @code{ordermagnitudep} orders numbers, constants, and constant expressions the same as @code{<},
1780 and all other elements the same as @code{orderlessp}.
1783 @c L: [%i, 1+%i, 2*x, minf, inf, %e, sin(1), 0, 1,2,3, 1.0, 1.0b0];
1784 @c sort (L, ordermagnitudep);
1785 @c sort (L, orderlessp);
1789 (%i1) L: [%i, 1+%i, 2*x, minf, inf, %e, sin(1), 0,1,2,3, 1.0, 1.0b0];
1790 (%o1) [%i, %i + 1, 2 x, minf, inf, %e, sin(1), 0, 1, 2, 3, 1.0,
1794 (%i2) sort (L, ordermagnitudep);
1795 (%o2) [minf, 0, sin(1), 1, 1.0, 1.0b0, 2, %e, 3, inf, %i,
1799 (%i3) sort (L, orderlessp);
1800 (%o3) [0, 1, 1.0, 2, 3, sin(1), 1.0b0, %e, %i, %i + 1, inf,
1805 @opencatbox{Categories:}
1810 @c -----------------------------------------------------------------------------
1812 @deffn {Function} sublist (@var{list}, @var{p})
1814 Returns the list of elements of @var{list} for which the predicate @code{p}
1815 returns @code{true}.
1820 @c L: [1, 2, 3, 4, 5, 6];
1821 @c sublist (L, evenp);
1825 (%i1) L: [1, 2, 3, 4, 5, 6];
1826 (%o1) [1, 2, 3, 4, 5, 6]
1829 (%i2) sublist (L, evenp);
1834 @opencatbox{Categories:}
1839 @c -----------------------------------------------------------------------------
1840 @anchor{sublist_indices}
1841 @deffn {Function} sublist_indices (@var{L}, @var{P})
1843 Returns the indices of the elements @code{x} of the list @var{L} for which
1844 the predicate @code{maybe(@var{P}(x))} returns @code{true};
1845 this excludes @code{unknown} as well as @code{false}.
1846 @var{P} may be the name of a function or a lambda expression.
1847 @var{L} must be a literal list.
1852 @c sublist_indices ('[a, b, b, c, 1, 2, b, 3, b],
1853 @c lambda ([x], x='b));
1854 @c sublist_indices ('[a, b, b, c, 1, 2, b, 3, b], symbolp);
1855 @c sublist_indices ([1 > 0, 1 < 0, 2 < 1, 2 > 1, 2 > 0],
1858 @c map (maybe, [x > 0, x < 0, x < -2]);
1859 @c sublist_indices ([x > 0, x < 0, x < -2], identity);
1863 (%i1) sublist_indices ('[a, b, b, c, 1, 2, b, 3, b],
1864 lambda ([x], x='b));
1868 (%i2) sublist_indices ('[a, b, b, c, 1, 2, b, 3, b], symbolp);
1869 (%o2) [1, 2, 3, 4, 7, 9]
1872 (%i3) sublist_indices ([1 > 0, 1 < 0, 2 < 1, 2 > 1, 2 > 0],
1877 (%i4) assume (x < -1);
1881 (%i5) map (maybe, [x > 0, x < 0, x < -2]);
1882 (%o5) [false, true, unknown]
1885 (%i6) sublist_indices ([x > 0, x < 0, x < -2], identity);
1890 @opencatbox{Categories:}
1895 @c -----------------------------------------------------------------------------
1897 @deffn {Function} tenth (@var{expr})
1899 Returns the 10th item of expression or list @var{expr}.
1900 See @mref{first} for more details.
1902 @opencatbox{Categories:}
1904 @category{Expressions}
1908 @c -----------------------------------------------------------------------------
1910 @deffn {Function} third (@var{expr})
1912 Returns the 3rd item of expression or list @var{expr}.
1913 See @mref{first} for more details.
1915 @opencatbox{Categories:}
1917 @category{Expressions}
1921 @c -----------------------------------------------------------------------------
1922 @c TREE_REDUCE ACCEPTS A SET OR LIST AS AN ARGUMENT, BUT RREDUCE AND LREDUCE WANT ONLY LISTS; STRANGE
1923 @anchor{tree_reduce}
1924 @deffn {Function} tree_reduce @
1925 @fname{tree_reduce} (@var{F}, @var{s}) @
1926 @fname{tree_reduce} (@var{F}, @var{s}, @var{s_0})
1928 Extends the binary function @var{F} to an n-ary function by composition,
1929 where @var{s} is a set or list.
1931 @code{tree_reduce} is equivalent to the following:
1932 Apply @var{F} to successive pairs of elements
1933 to form a new list @code{[@var{F}(@var{s_1}, @var{s_2}), @var{F}(@var{s_3}, @var{s_4}), ...]},
1934 carrying the final element unchanged if there are an odd number of elements.
1935 Then repeat until the list is reduced to a single element, which is the return value.
1937 When the optional argument @var{s_0} is present,
1938 the result is equivalent @code{tree_reduce(@var{F}, cons(@var{s_0}, @var{s}))}.
1940 For addition of floating point numbers,
1941 @code{tree_reduce} may return a sum that has a smaller rounding error
1942 than either @code{rreduce} or @code{lreduce}.
1944 The elements of @var{s} and the partial results may be arranged in a minimum-depth binary tree,
1945 thus the name "tree_reduce".
1949 @code{tree_reduce} applied to a list with an even number of elements.
1952 @c tree_reduce (f, [a, b, c, d]);
1956 (%i1) tree_reduce (f, [a, b, c, d]);
1957 (%o1) f(f(a, b), f(c, d))
1961 @code{tree_reduce} applied to a list with an odd number of elements.
1964 @c tree_reduce (f, [a, b, c, d, e]);
1968 (%i1) tree_reduce (f, [a, b, c, d, e]);
1969 (%o1) f(f(f(a, b), f(c, d)), e)
1973 @opencatbox{Categories:}
1980 @c -----------------------------------------------------------------------------
1982 @deffn {Function} unique (@var{L})
1984 Returns the unique elements of the list @var{L}.
1986 When all the elements of @var{L} are unique,
1987 @code{unique} returns a shallow copy of @var{L},
1990 If @var{L} is not a list, @code{unique} returns @var{L}.
1995 @c unique ([1, %pi, a + b, 2, 1, %e, %pi, a + b, [1]]);
1999 (%i1) unique ([1, %pi, a + b, 2, 1, %e, %pi, a + b, [1]]);
2000 (%o1) [1, 2, %e, %pi, [1], b + a]
2005 @c -----------------------------------------------------------------------------
2006 @c XREDUCE ACCEPTS A SET OR LIST AS AN ARGUMENT, BUT RREDUCE AND LREDUCE WANT ONLY LISTS; STRANGE
2008 @deffn {Function} xreduce @
2009 @fname{xreduce} (@var{F}, @var{s}) @
2010 @fname{xreduce} (@var{F}, @var{s}, @var{s_0})
2012 Extends the function @var{F} to an n-ary function by composition,
2013 or, if @var{F} is already n-ary, applies @var{F} to @var{s}.
2014 When @var{F} is not n-ary, @code{xreduce} is the same as @code{lreduce}.
2015 The argument @var{s} is a list.
2017 Functions known to be n-ary include
2018 addition @code{+}, multiplication @code{*}, @code{and}, @code{or}, @code{max},
2019 @code{min}, and @code{append}.
2020 Functions may also be declared n-ary by @code{declare(@var{F}, nary)}.
2021 For these functions,
2022 @code{xreduce} is expected to be faster than either @code{rreduce} or @code{lreduce}.
2024 When the optional argument @var{s_0} is present,
2025 the result is equivalent to @code{xreduce(@var{s}, cons(@var{s_0}, @var{s}))}.
2027 @c NOT SURE WHAT IS THE RELEVANCE OF THE FOLLOWING COMMENT
2028 @c MAXIMA IS NEVER SO CAREFUL ABOUT FLOATING POINT ASSOCIATIVITY SO FAR AS I KNOW
2029 Floating point addition is not exactly associative; be that as it may,
2030 @code{xreduce} applies Maxima's n-ary addition when @var{s} contains floating point numbers.
2034 @code{xreduce} applied to a function known to be n-ary.
2035 @code{F} is called once, with all arguments.
2038 @c declare (F, nary);
2040 @c xreduce (F, [a, b, c, d, e]);
2044 (%i1) declare (F, nary);
2052 (%i3) xreduce (F, [a, b, c, d, e]);
2053 (%o3) [a, b, c, d, e]
2057 @code{xreduce} applied to a function not known to be n-ary.
2058 @code{G} is called several times, with two arguments each time.
2062 @c xreduce (G, [a, b, c, d, e]);
2063 @c lreduce (G, [a, b, c, d, e]);
2071 (%i2) xreduce (G, [a, b, c, d, e]);
2072 (%o2) [[[[a, b], c], d], e]
2075 (%i3) lreduce (G, [a, b, c, d, e]);
2076 (%o3) [[[[a, b], c], d], e]
2080 @opencatbox{Categories:}
2087 @c h-----------------------------------------------------------------------------
2088 @node Performance considerations for Lists, ,Functions and Variables for Lists, Lists
2089 @subsection Performance considerations for Lists
2090 @c -----------------------------------------------------------------------------
2092 Lists provide efficient ways of appending and removing elements.
2093 They can be created without knowing their final dimensions.
2094 Lisp provides efficient means of copying and handling lists.
2095 Also nested lists do not need to be strictly rectangular.
2096 These advantages over declared arrays come with the drawback that the amount of time
2097 needed for accessing a random element within a list may be roughly
2098 proportional to the element's distance from its beginning.
2099 Efficient traversal of lists is still possible, though, by using the list as a
2103 @c l:[Test,1,2,3,4];
2109 (%i1) l:[Test,1,2,3,4];
2110 (%o1) [Test, 1, 2, 3, 4]
2112 (%i2) while l # [] do
2126 Another even faster example would be:
2128 @c l:[Test,1,2,3,4];
2134 (%i1) l:[Test,1,2,3,4];
2135 (%o1) [Test, 1, 2, 3, 4]
2152 Beginning traversal with the last element of a list is possible after
2153 reversing the list using @code{reverse ()}.
2154 If the elements of a long list need to be processed in a different
2155 order performance might be increased by converting the list into a
2156 declared array first.
2158 Note also that the ending condition of @code{for} loops
2159 is tested for every iteration which means that the result of a
2160 @code{length} should be cached if it is used in the ending
2164 @c l:makelist(i,i,1,100000)$
2167 @c for i:1 thru lngth do
2172 (%i1) l:makelist(i,i,1,100000)$
2174 (%i2) lngth:length(l);
2182 (%i4) for i:1 thru lngth do