up previous next

Making Lists

The following example illustrates the use of lists in CoCoA.

example

    
L := [2,3,"a string",[5,7],3,3];  -- L is now a list
L[3];  -- here is the 3rd component of L
a string
-------------------------------
L[4];  -- the 4th component of L is a list, itself
[5, 7]
-------------------------------
L[4][2];  -- the 2nd component of the 4th component of L
7
-------------------------------
L[4,2];  -- same as above
7
-------------------------------
Append(L,"new");
L;
[2, 3, "a string", [5, 7], 3, 3, "new"]
-------------------------------
-- insert 8 as the 4th component of L, shifting the other
-- entries to the right:
Insert(L,4,8);  
L;
[2, 3, "a string", 8, [5, 7], 3, 3, "new"]
-------------------------------
Remove(L,4);  -- remove it again
L;
[2, 3, "a string", [5, 7], 3, 3, "new"]
-------------------------------
Len(L);  -- the number of components of L
7
-------------------------------
Set(L);  -- same as L but with repeats removed
[2, 3, "a string", [5, 7], "new"]
-------------------------------
1..5;  -- a range of values
[1, 2, 3, 4, 5]
-------------------------------
[ X^2 | X In 1..5];  -- a useful way to make lists
[1, 4, 9, 16, 25]
-------------------------------
[1,2] >< [3,4] >< [5];  -- Cartesian product: use a greater-than 
                        -- sign ">" and a less-than sign "<" to make
                        -- the operator "><".
[[1, 3, 5], [1, 4, 5], [2, 3, 5], [2, 4, 5]]
-------------------------------