up previous next
CartesianProduct, CartesianProductList    --    Cartesian product of lists


Syntax
CartesianProduct(L1: LIST, L2: LIST, L3: LIST, ..): LIST
CartesianProductList(L: LIST of LIST): LIST
L1 >< L2
L1 >< L2 >< ... >< Ln

where each Li is a LIST

Description
This command returns the list whose elements form the Cartesian product of L_1,...,L_n.

For the N-fold product of a list with itself, one may use tuples .

Example
/**/  L1 := [1,2,3];
/**/  L2 := ["a","b"];
/**/  L1 >< L2 >< [5];  -- same as
/**/  CartesianProduct(L1, L2, [5]);  -- same as
/**/  CartesianProductList([L1, L2, [5]]);  -- this takes a list of lists
[[1, "a", 5], [1, "b", 5], [2, "a", 5], [2, "b", 5], [3, "a", 5], [3, "b", 5]]
-------------------------------
/**/  ChessBoard := (1..8)><(1..8);  -- Need brackets around 1..8 otherwise
                                           -- we get a parse error.
Be careful: in CoCoA the operator >< denotes cartesian product, whereas the operator <> is used for not equal.
See Also