[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

37. Lists


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

37.1 Introduction to Lists

Lists are the basic building block for Maxima and Lisp. All data types other than arrays, hash tables, numbers are represented as Lisp lists, These Lisp lists have the form

((MPLUS) $A 2)

to indicate an expression a+2. At Maxima level one would see the infix notation a+2. Maxima also has lists which are printed as

[1, 2, 7, x+y]

for a list with 4 elements. Internally this corresponds to a Lisp list of the form

((MLIST) 1  2  7  ((MPLUS)  $X $Y ))

The flag which denotes the type field of the Maxima expression is a list itself, since after it has been through the simplifier the list would become

((MLIST SIMP) 1 2 7 ((MPLUS SIMP) $X $Y))


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

37.2 Functions and Variables for Lists

Function: append (list_1, ..., list_n)

Returns a single list of the elements of list_1 followed by the elements of list_2, ... append also works on general expressions, e.g. append (f(a,b), f(c,d,e)); yields f(a,b,c,d,e).

Do example(append); for an example.

Categories:  Lists · Expressions

Function: assoc (key, list, default)
Function: assoc (key, list)

This function searches for the key in the left hand side of the input list of the form [x,y,z,...] where each of the list elements is an expression of a binary operand and 2 elements. For example x=1, 2^3, [a,b] etc. The key is checked againts the first operand. assoc returns the second operand if the key is found. If the key is not found it either returns the default value. default is optional and defaults to false.

Categories:  Lists · Expressions

Function: atom (expr)

Returns true if expr is atomic (i.e. a number, name or string) else false. Thus atom(5) is true while atom(a[1]) and atom(sin(x)) are false (asuming a[1] and x are unbound).

Categories:  Expressions · Predicate functions

Function: cons (expr, list)

Returns a new list constructed of the element expr as its first element, followed by the elements of list. cons also works on other expressions, e.g. cons(x, f(a,b,c)); -> f(x,a,b,c).

Categories:  Lists · Expressions

Function: copylist (list)

Returns a copy of the list list.

Categories:  Lists

Function: create_list (form, x_1, list_1, ..., x_n, list_n)

Create a list by evaluating form with x_1 bound to each element of list_1, and for each such binding bind x_2 to each element of list_2, .... The number of elements in the result will be the product of the number of elements in each list. Each variable x_i must actually be a symbol - it will not be evaluated. The list arguments will be evaluated once at the beginning of the iteration.

(%i1) create_list(x^i,i,[1,3,7]);
                 3   7
(%o1)       [x, x , x ]

With a double iteration:

(%i1) create_list([i,j],i,[a,b],j,[e,f,h]);
(%o1) [[a, e], [a, f], [a, h], [b, e], [b, f], [b, h]]

Instead of list_i two args may be supplied each of which should evaluate to a number. These will be the inclusive lower and upper bounds for the iteration.

(%i1) create_list([i,j],i,[1,2,3],j,1,i);
(%o1) [[1, 1], [2, 1], [2, 2], [3, 1], [3, 2], [3, 3]]

Note that the limits or list for the j variable can depend on the current value of i.

Categories:  Lists

Function: delete (expr_1, expr_2)
Function: delete (expr_1, expr_2, n)

delete(expr_1, expr_2) removes from expr_2 any arguments of its top-level operator which are the same (as determined by "=") as expr_1. Note that "=" tests for formal equality, not equivalence. Note also that arguments of subexpressions are not affected.

expr_1 may be an atom or a non-atomic expression. expr_2 may be any non-atomic expression. delete returns a new expression; it does not modify expr_2.

delete(expr_1, expr_2, n) removes from expr_2 the first n arguments of the top-level operator which are the same as expr_1. If there are fewer than n such arguments, then all such arguments are removed.

Examples:

Removing elements from a list.

(%i1) delete (y, [w, x, y, z, z, y, x, w]);
(%o1)                  [w, x, z, z, x, w]

Removing terms from a sum.

(%i1) delete (sin(x), x + sin(x) + y);
(%o1)                         y + x

Removing factors from a product.

(%i1) delete (u - x, (u - w)*(u - x)*(u - y)*(u - z));
(%o1)                (u - w) (u - y) (u - z)

Removing arguments from an arbitrary expression.

(%i1) delete (a, foo (a, b, c, d, a));
(%o1)                     foo(b, c, d)

Limit the number of removed arguments.

(%i1) delete (a, foo (a, b, a, c, d, a), 2);
(%o1)                    foo(b, c, d, a)

Whether arguments are the same as expr_1 is determined by "=". Arguments which are equal but not "=" are not removed.

(%i1) [is (equal (0, 0)), is (equal (0, 0.0)), is (equal (0, 0b0))];

`rat' replaced 0.0 by 0/1 = 0.0
`rat' replaced 0.0B0 by 0/1 = 0.0B0
(%o1)                  [true, true, true]
(%i2) [is (0 = 0), is (0 = 0.0), is (0 = 0b0)];
(%o2)                 [true, false, false]
(%i3) delete (0, [0, 0.0, 0b0]);
(%o3)                     [0.0, 0.0b0]
(%i4) is (equal ((x + y)*(x - y), x^2 - y^2));
(%o4)                         true
(%i5) is ((x + y)*(x - y) = x^2 - y^2);
(%o5)                         false
(%i6) delete ((x + y)*(x - y), [(x + y)*(x - y), x^2 - y^2]);
                              2    2
(%o6)                       [x  - y ]

Categories:  Lists · Expressions

Function: eighth (expr)

Returns the 8'th item of expression or list expr. See first for more details.

Categories:  Lists · Expressions

Function: endcons (expr, list)

Returns a new list consisting of the elements of list followed by expr. endcons also works on general expressions, e.g. endcons(x, f(a,b,c)); -> f(a,b,c,x).

Categories:  Lists · Expressions

Function: fifth (expr)

Returns the 5'th item of expression or list expr. See first for more details.

Categories:  Lists · Expressions

Function: first (expr)

Returns the first part of expr which may result in the first element of a list, the first row of a matrix, the first term of a sum, etc. Note that first and its related functions, rest and last, work on the form of expr which is displayed not the form which is typed on input. If the variable inflag is set to true however, these functions will look at the internal form of expr. Note that the simplifier re-orders expressions. Thus first(x+y) will be x if inflag is true and y if inflag is false (first(y+x) gives the same results). The functions second .. tenth yield the second through the tenth part of their input argument.

Categories:  Lists · Expressions

Function: fourth (expr)

Returns the 4'th item of expression or list expr. See first for more details.

Categories:  Lists · Expressions

Function: get (a, i)

Retrieves the user property indicated by i associated with atom a or returns false if a doesn't have property i.

get evaluates its arguments.

(%i1) put (%e, 'transcendental, 'type);
(%o1)                    transcendental
(%i2) put (%pi, 'transcendental, 'type)$
(%i3) put (%i, 'algebraic, 'type)$
(%i4) typeof (expr) := block ([q],
        if numberp (expr)
        then return ('algebraic),
        if not atom (expr)
        then return (maplist ('typeof, expr)),
        q: get (expr, 'type),
        if q=false
        then errcatch (error(expr,"is not numeric.")) else q)$
(%i5) typeof (2*%e + x*%pi);
x is not numeric.
(%o5)  [[transcendental, []], [algebraic, transcendental]]
(%i6) typeof (2*%e + %pi);
(%o6)     [transcendental, [algebraic, transcendental]]

Function: join (l, m)

Creates a new list containing the elements of lists l and m, interspersed. The result has elements [l[1], m[1], l[2], m[2], ...]. The lists l and m may contain any type of elements.

If the lists are different lengths, join ignores elements of the longer list.

Maxima complains if l or m is not a list.

Examples:

(%i1) L1: [a, sin(b), c!, d - 1];
(%o1)                [a, sin(b), c!, d - 1]
(%i2) join (L1, [1, 2, 3, 4]);
(%o2)          [a, 1, sin(b), 2, c!, 3, d - 1, 4]
(%i3) join (L1, [aa, bb, cc, dd, ee, ff]);
(%o3)        [a, aa, sin(b), bb, c!, cc, d - 1, dd]

Categories:  Lists

Function: last (expr)

Returns the last part (term, row, element, etc.) of the expr.

Categories:  Lists · Expressions

Function: length (expr)

Returns (by default) the number of parts in the external (displayed) form of expr. For lists this is the number of elements, for matrices it is the number of rows, and for sums it is the number of terms (see dispform).

The length command is affected by the inflag switch. So, e.g. length(a/(b*c)); gives 2 if inflag is false (Assuming exptdispflag is true), but 3 if inflag is true (the internal representation is essentially a*b^-1*c^-1).

Categories:  Lists · Expressions

Option variable: listarith

default value: true - if false causes any arithmetic operations with lists to be suppressed; when true, list-matrix operations are contagious causing lists to be converted to matrices yielding a result which is always a matrix. However, list-list operations should return lists.

Categories:  Lists · Global flags

Function: listp (expr)

Returns true if expr is a list else false.

Categories:  Lists · Predicate functions

Function: makelist (expr, i, i_0, i_1)
Function: makelist (expr, x, list)

Constructs and returns a list, each element of which is generated from expr.

makelist (expr, i, i_0, i_1) returns a list, the j'th element of which is equal to ev (expr, i=j) for j equal to i_0 through i_1.

makelist (expr, x, list) returns a list, the j'th element of which is equal to ev (expr, x=list[j]) for j equal to 1 through length (list).

Examples:

(%i1) makelist(concat(x,i),i,1,6);
(%o1)               [x1, x2, x3, x4, x5, x6]
(%i2) makelist(x=y,y,[a,b,c]);
(%o2)                 [x = a, x = b, x = c]

Categories:  Lists

Function: member (expr_1, expr_2)

Returns true if is(expr_1 = a) for some element a in args(expr_2), otherwise returns false.

expr_2 is typically a list, in which case args(expr_2) = expr_2 and is(expr_1 = a) for some element a in expr_2 is the test.

member does not inspect parts of the arguments of expr_2, so it may return false even if expr_1 is a part of some argument of expr_2.

See also elementp.

Examples:

(%i1) member (8, [8, 8.0, 8b0]);
(%o1)                         true
(%i2) member (8, [8.0, 8b0]);
(%o2)                         false
(%i3) member (b, [a, b, c]);
(%o3)                         true
(%i4) member (b, [[a, b], [b, c]]);
(%o4)                         false
(%i5) member ([b, c], [[a, b], [b, c]]);
(%o5)                         true
(%i6) F (1, 1/2, 1/4, 1/8);
                               1  1  1
(%o6)                     F(1, -, -, -)
                               2  4  8
(%i7) member (1/8, %);
(%o7)                         true
(%i8) member ("ab", ["aa", "ab", sin(1), a + b]);
(%o8)                         true

Categories:  Lists · Expressions · Predicate functions

Function: ninth (expr)

Returns the 9'th item of expression or list expr. See first for more details.

Categories:  Lists · Expressions

Function: unique (L)

Returns the unique elements of the list L.

When all the elements of L are unique, unique returns a shallow copy of L, not L itself.

If L is not a list, unique returns L.

Example:

(%i1) unique ([1, %pi, a + b, 2, 1, %e, %pi, a + b, [1]]);
(%o1)              [1, 2, %e, %pi, [1], b + a]

Function: rest (expr, n)
Function: rest (expr)

Returns expr with its first n elements removed if n is positive and its last - n elements removed if n is negative. If n is 1 it may be omitted. expr may be a list, matrix, or other expression.

Categories:  Lists · Expressions

Function: reverse (list)

Reverses the order of the members of the list (not the members themselves). reverse also works on general expressions, e.g. reverse(a=b); gives b=a.

Categories:  Lists · Expressions

Function: second (expr)

Returns the 2'nd item of expression or list expr. See first for more details.

Categories:  Lists · Expressions

Function: seventh (expr)

Returns the 7'th item of expression or list expr. See first for more details.

Categories:  Lists · Expressions

Function: sixth (expr)

Returns the 6'th item of expression or list expr. See first for more details.

Categories:  Lists · Expressions

Function: sublist_indices (L, P)

Returns the indices of the elements x of the list L for which the predicate maybe(P(x)) returns true; this excludes unknown as well as false. P may be the name of a function or a lambda expression. L must be a literal list.

Examples:

(%i1) sublist_indices ('[a, b, b, c, 1, 2, b, 3, b],
                       lambda ([x], x='b));
(%o1)                     [2, 3, 7, 9]
(%i2) sublist_indices ('[a, b, b, c, 1, 2, b, 3, b], symbolp);
(%o2)                  [1, 2, 3, 4, 7, 9]
(%i3) sublist_indices ([1 > 0, 1 < 0, 2 < 1, 2 > 1, 2 > 0],
                       identity);
(%o3)                       [1, 4, 5]
(%i4) assume (x < -1);
(%o4)                       [x < - 1]
(%i5) map (maybe, [x > 0, x < 0, x < -2]);
(%o5)                [false, true, unknown]
(%i6) sublist_indices ([x > 0, x < 0, x < -2], identity);
(%o6)                          [2]

Categories:  Lists

Function: tenth (expr)

Returns the 10'th item of expression or list expr. See first for more details.

Categories:  Lists · Expressions

Function: third (expr)

Returns the 3'rd item of expression or list expr. See first for more details.

Categories:  Lists · Expressions


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by root on June, 23 2010 using texi2html 1.76.