List

A list is a one dimensional array of data values of any different kinds. It is created with a sequence of elements separated by commas, and surrounded by a pair of parentheses. For instance
x = (2, 3, "My goldfish is evil");

Any data value can be a list element, including a list. For example

x = (2, [3, 5], x -> sqrt(x^2 + 1), "My goldfish is evil", ("a", "b", "c"));
Here the second element is a matrix, the third element is a function, the fourth element is a string, and the last element is a list.

To access any element, use the operator # followed by the index. The index value must be an integer starting from 1. For example, the third element is

x#3

Several elements of a list can be indexed at once using a vector index. The result is a still a list. For example

x#[1:3]
gives a list of the first three elements of the list, while
x#[1:2:9]
returns the list of odd-indexed elements up to the ninth.

To check the length of a list, one can use # followed by the list variable name

>> x = (2, 3, "My goldfish is evil");
>> #x
   3
x.length would achieve the same thing. However, if x is a vector of three elements, x.length would also return 3, while # x returns 1.

The last element of a list can be retrieved using the index $, and the next last using $-1, etc.

Note that Shang is vectorized on the object level. Therefore a list is not a so-called ``container'' in other languages. Any value that is not a list, is actually a list of one item. For example, the number -3 is also a list of one number -3.

x=-3;
y=(-3);
x == y
    1
Likewise, -3, (-3), ((-3)), and (((-3))) are all the same. And it's impossible to distinguish (a, b) from ((a, b)).



Subsections
oz 2009-12-22