Cartesian product of two or more lists: Difference between revisions

Content added Content deleted
m (Minor code adjustment.)
(Added a reference to the procedure "product" in the standard module "algorithm".)
Line 2,054: Line 2,054:
====Recursive procedure====
====Recursive procedure====
As iterators cannot be recursive, we have to use a procedure which returns the whole sequence. And as we don’t know the number of sequences, we use a “varargs”. So all the sequences must contain the same type of values, values which are returned as sequences and not tuples.
As iterators cannot be recursive, we have to use a procedure which returns the whole sequence. And as we don’t know the number of sequences, we use a “varargs”. So all the sequences must contain the same type of values, values which are returned as sequences and not tuples.

Note that there exists in the standard module “algorithm” a procedure which computes the product of sequences of a same type. It is not recursive and, so, likely more efficient that the following version.


<lang Nim>proc product[T](a: varargs[seq[T]]): seq[seq[T]] =
<lang Nim>proc product[T](a: varargs[seq[T]]): seq[seq[T]] =