Cartesian product of two or more lists: Difference between revisions
Content added Content deleted
Line 441: | Line 441: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<lang lisp>(defun cartesian-product (l) |
<lang lisp>(defun n-cartesian-product (l) |
||
"Compute the n-cartesian product of a list of sets represented as list with a mixed recursive-iterative approach. |
"Compute the n-cartesian product of a list of sets represented as list with a mixed recursive-iterative approach. |
||
Algorithm: |
Algorithm: |
||
Line 450: | Line 450: | ||
(list nil) |
(list nil) |
||
(loop for x in (car l) |
(loop for x in (car l) |
||
nconc (loop for y in (cartesian-product (cdr l)) |
nconc (loop for y in (n-cartesian-product (cdr l)) |
||
collect (cons x y)))))</lang> |
collect (cons x y)))))</lang> |
||