Lists can be entered in Mathics3 with curly braces {
and }
:
mylist = {a, b, c, d}
There are various functions for constructing lists:
Range[5]
Array[f, 4]
ConstantArray[x, 4]
Table[n ^ 2, {n, 2, 5}]
The number of elements of a list can be determined with Length
:
Length[mylist]
Elements can be extracted using double square braces:
mylist[[3]]
Negative indices count from the end:
mylist[[-3]]
Lists can be nested:
mymatrix = {{1, 2}, {3, 4}, {5, 6}};
There are alternate forms to display lists:
TableForm[mymatrix]
MatrixForm[mymatrix]
There are various ways of extracting elements from a list:
mymatrix[[2, 1]]
mymatrix[[;;, 2]]
Take[mylist, 3]
Take[mylist, -2]
Drop[mylist, 2]
First[mymatrix]
Last[mylist]
Most[mylist]
Rest[mylist]
Lists can be used to assign values to multiple variables at once:
{a, b} = {1, 2};
a
b
Operations like addition and multiplication, “thread” over lists; lists are combined element-wise:
{1, 2, 3} + {4, 5, 6}
{1, 2, 3} * {4, 5, 6}
It is an error to combine lists with unequal lengths:
{1, 2} + {4, 5, 6}