[next][previous][up][top][index]
search for:

lists -- an overview

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}

o1 = {a, b, c, d, e}

o1 : List

We retrieve the length of a list with the operator #.

i2 : #x

o2 = 5

We use it also to obtain a particular element. The elements are nubered consecutively starting with 0.

i3 : x#2

o3 = c

o3 : Symbol

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

o4 = (a, e)

o4 : Sequence

Omitting an element of a list causes the symbol null to be inserted in its place.

i5 : g = {3,4,,5}

o5 = {3, 4, , 5}

o5 : List
i6 : peek g

o6 = {3,4,null,5}

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}

o7 = {30001, 40002, 50003}

o7 : List

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}}}

o8 = {{a, b, c}, {d, {e, f}}}

o8 : List
i9 : #y

o9 = 2

One level of nesting may be eliminated with flatten.

i10 : flatten y

o10 = {a, b, c, d, {e, f}}

o10 : List

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}}

o11 = {{a, 1}, {b, 2}, {c, 3}}

o11 : List
i12 : isTable z

o12 = true
i13 : MatrixExpression z

o13 = | a  1 |
      |      |
      | b  2 |
      |      |
      | c  3 |

o13 : MatrixExpression

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.


[next][previous][up][top][index]
search for: