Cartesian product of two or more lists: Difference between revisions

Content added Content deleted
Line 460: Line 460:
</lang>
</lang>


'''Extra credit:'''


<lang lisp>(defun n-cartesian-product (l)
<lang lisp>(defun n-cartesian-product (l)
Line 473: Line 474:
collect (cons x y)))))</lang>
collect (cons x y)))))</lang>


'''Output:'''
Example of use:


<lang lisp>CL-USER> (cartesian-product '((1 2 3) (30) (500 100)))
<lang lisp>CL-USER> (n-cartesian-product '((1776 1789) (7 12) (4 14 23) (0 1)))
((1776 7 4 0) (1776 7 4 1) (1776 7 14 0) (1776 7 14 1) (1776 7 23 0) (1776 7 23 1) (1776 12 4 0) (1776 12 4 1) (1776 12 14 0) (1776 12 14 1) (1776 12 23 0) (1776 12 23 1) (1789 7 4 0) (1789 7 4 1) (1789 7 14 0) (1789 7 14 1) (1789 7 23 0) (1789 7 23 1) (1789 12 4 0) (1789 12 4 1) (1789 12 14 0) (1789 12 14 1) (1789 12 23 0) (1789 12 23 1))
CL-USER> (n-cartesian-product '((1 2 3) (30) (500 100)))
((1 30 500) (1 30 100) (2 30 500) (2 30 100) (3 30 500) (3 30 100))
((1 30 500) (1 30 100) (2 30 500) (2 30 100) (3 30 500) (3 30 100))
CL-USER> (n-cartesian-product '((1 2 3) () (500 100)))
NIL
</lang>
</lang>