MATHEMATICA LISTS:
Structured data objects
Wed, 22 Jan 2003

List groups a collection of 
expressions of any type.

lexp  =  {a, b, c}

We have just defined a Symbol lexp to be the List with elements a, b, c, which are themselves symbols.   We can then do operations on them.

lexp^2

Sin[{a, b, c}]

Many building functions having the above property are called listable.  By default, your own function will not be listable unless you change the attributes of your function.

Attributes[Sin]

?SetAttributes

List can be used as vectors (we can add as long as they are of same size):

{a, b, c}  +  {1, 2, 3}

{x, y, z}  =  {1, 2, 3}

This assigned 1, 2, 3 to x, y, z, respectively.

Head[lexp]

First[lexp]

Last[lexp]

To refer to the i-th element in a list, we use the double square brackets.   (But this works only if the list has already created.)

lexp[[2]]

In Mathematica, the elements in a list are refers from 1, 2, ....

Length[lexp]

lexp[[0]]

Head[lexp]  ==  lexp[[0]]

The head of a list or the zeroth element of a list is its data type List.

lexp[[-1]]

rexp  =  {1, 2, 3}

Join[lexp,  rexp]

Append[lexp,  1]

Append[ ] or Prepend[ ] does not change the input expression lexp, but creates a new list with an element appended or prepended.   You can do assignment lexp=Append[lexp, a] that changes lexp.

lexp

Prepend[lexp,  1]

List[a, b, c]

The FullForm (i.e. internal form) of the list { ... } is List[ ...].  Thus List[a,b,c] and {a,b,c} are totally equivalent.  The member of the elements of a list can be anything:

{a,  2,  1/3,  1 + x^2,  {a},  {{}}}

Lists are used to represent 
matrices as list of lists.

m = {{1, 2}, {0.5, 3}}

MatrixForm[m]

Eigenvalues[m]

Det[m]

Transpose[m]

Inverse[m]

m  .  m

This is how a matrix multiply a vector:

m  .  {a,  b}

LinearSolve[m, {b1, b2}]  // Simplify

To access the elements of an array, use [[i,j]], as

m[[1, 2]]

This gives you the first row.

m[[1]]

How to get the first column of m?

Other List Manipulation Functions

l  =  {a, b, c, d}

{RotateLeft[l],  RotateRight[l]}

l

Note that l itself is untouched by the functions.

Delete[l, 1]

Insert[l, i, 3]

MemberQ[l, i]

Take[l, 2]

Drop[l, 2]

List can be used to represent a tree.

The list can be nested in arbitrary ways.

l  =  { {a,  b},  {{Sin[x],  1},  2}}

MatrixForm[l]

TreeForm[l]

List (or list of lists) v.s. associative array

To use a list or array, the elements, size etc has to be defined first.  For example, the following use of two-dimensional array is not correct (assuming we have not referred to h before):

h[[1, 2]]  =  3

Since h as a list of list does not exist yet.  We should say:

(h  =  Table[0,  {5}, {5}])  //  MatrixForm

h[[1, 2]]  =  3;  MatrixForm[h]

We can also create a list by the Table[ ] function:

l  =  Table[a[i],  {i, 0, 4}]

l  =  Range[4]

Associative Array

For associative array, you need not specify the size of array first.  In fact, it is internally implemented as a hashtable.  We use single square brackets for associative array.

a[0]  =  2;  a[-2]  =  -4;  a[1000]  =  w;  
    a[7, 7]  =  7;  a[star]  =  5

?a

Mathematica Expressions

Mathematica expressions are internally always the form

expr[expr1,  expr2,   ... ]

Where expr is called the Head, and expr1 is the first element of the expression, expr2 2nd element etc.   We obtain the expr by applying the Head[ ...] function, we obtain expr1 by First[...] or by expr[[1]].   Everything that mathematica executes is an expression.
All the list manipulation functions also work for arbitrary expressions.

Head[a + b]

Why?

First[a + b]

FullForm[a + b]

Plus[a, b]

FullForm[a*b  +  c (1 + x)^3]

This data structure can be understood as a tree.

Assignment and Evaluation

a = 1

a + b

b  =  (1 + x)^2

a + b

To force evaluation, we use Evaluate[ ] (without it the following will not work).

Plot[ Evaluate[Table[x^i, {i, 1, 4}]],  {x, 0, 1}]

Somewhat opposite to Evaluate[ ] is Hold[ ].

x  =  1;
y  =  Hold[x]  +  1

ReleaseHold[y]

Principle of Mathematica expression evaluation:  Apply transformation rules repeatedly until result no longer change.

Clear[a, b]

a + b

Remove[a, b]

This show you what variables (symbols) have been defined so far.

?Global`*

READINGS:
section on lists (1.8) section on expressions (2.1).