up previous next
1.1.7 Tutorial: lists
A convenient way of "putting together" many values in CoCoA-5 is to put them into a LIST. Though the CoCoA-5 name is LIST the data-structure more closely resembles a vector in C++ than a list in C++.

CoCoA-5 does not impose restrictions on the values a LIST may contain; nevertheless it usually makes most sense if the values are all of the same type (e.g. all integers, all polynomials).

A list may be created in several ways. The simplest is to write out the entries explicitly. Another is to start with an empty list, and the append new elements in a loop. There is also a convenient syntax inspired by the mathematical notation for sets.

If you have a list, you can find out how many elements it contains using the function len. You can also iterate over the elements of a list using the foreach loop command.

Example
/**/ [2,3,5];  // list containing the elements 2,3,5 in that order
[2, 3, 5]
/**/ 1..5;  // integer range
[1, 2, 3, 4, 5]
// Now create a list using "append":
/**/ L := [];  // start with the empty list in L
/**/ for i := 1 to 5 do append(ref L, i^2); endfor;
/**/ println L;
[1, 4, 9, 16, 25]
/**/ [ n in L | IsEven(n)];  // list containing even values
[4, 16]
/**/ [ n^2 | n in L  and  n < 10];
[1, 16, 81]
/**/ len(L);
5
/**/ foreach n in L do print n," "; endforeach;
1 4 9 16 25