# Lists

## 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))
```

## Definitions 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.

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`.

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).

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)`.

Function: copylist (list)
Returns a copy of the list list.

Function: delete (expr_1, expr_2)
Function: delete (expr_1, expr_2, n)
Removes all occurrences of expr_1 from expr_2. expr_1 may be a term of expr_2 (if it is a sum) or a factor of expr_2 (if it is a product).

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

```

`delete(expr_1, expr_2, n)` removes the first n occurrences of expr_1 from expr_2. If there are fewer than n occurrences of expr_1 in expr_2 then all occurrences will be deleted.

```(%i1) delete(a, f(a,b,c,d,a));
(%o1)                      f(b, c, d)
(%i2) delete(a, f(a,b,a,c,d,a), 2);
(%o2)                     f(b, c, d, a)

```

Function: eighth (expr)
Returns the 8'th item of expression or list expr. See `first` for more details.

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)`.

Function: fifth (expr)
Returns the 5'th item of expression or list expr. See `first` for more details.

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.

Function: fourth (expr)
Returns the 4'th item of expression or list expr. See `first` for more details.

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: last (expr)
Returns the last part (term, row, element, etc.) of the expr.

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`).

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.

Function: listp (expr)
Returns `true` if expr is a list else `false`.

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]

```

Function: member (expr, list)
Returns `true` if expr occurs as a member of list (not within a member). Otherwise `false` is returned. `member` also works on non-list expressions, e.g. `member(b,f(a,b,c));` -> `true`.

Function: ninth (expr)
Returns the 9'th item of expression or list expr. See `first` for more details.

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.

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`.

Function: second (expr)
Returns the 2'nd item of expression or list expr. See `first` for more details.

Function: seventh (expr)
Returns the 7'th item of expression or list expr. See `first` for more details.

Function: sixth (expr)
Returns the 6'th item of expression or list expr. See `first` for more details.

Function: tenth (expr)
Returns the 10'th item of expression or list expr. See `first` for more details.

Function: third (expr)
Returns the 3'rd item of expression or list expr. See `first` for more details.