A list is a handy way to store a series of things. We create one by separating the elements of the series by commas and surrounding the series with braces.
i1 : x = {a,b,c,d,e} |
We retrieve the length of a list with the operator #.
i2 : #x |
We use it also to obtain a particular element. The elements are nubered consecutively starting with 0.
i3 : x#2 |
The elements of a list are stored in consecutive memory locations, so the operation above is fast.
The functions first and last retrieve the first and last elements of a list.
i4 : first x, last x |
Omitting an element of a list causes the symbol null to be inserted in its place.
i5 : g = {3,4,,5} |
i6 : peek g |
Lists can be used as vectors, provided their elements are the sorts of things that can be added and mutliplied.
i7 : 10000*{3,4,5} + {1,2,3} |
If the elements of a list are themselves lists, we say that we have a nested list.
i8 : y = {{a,b,c},{d,{e,f}}} |
i9 : #y |
One level of nesting may be eliminated with flatten.
i10 : flatten y |
A table is a list whose elements are lists all of the same length. The inner lists are regarded as rows when the table is displayed as a two-dimensional array with MatrixExpression.
i11 : z = {{a,1},{b,2},{c,3}} |
i12 : isTable z |
i13 : MatrixExpression z |
Various other functions for manipulating lists include {...} | {...}, append, between, delete, drop, join, mingle, pack, prepend, reverse, rsort, sort, take, and unique.
The class of all lists is List, and the class of all basic lists, useful for deriving news types of list that do not inherit methods for treating lists as vectors, is BasicList.