Cartesian product of two or more lists: Difference between revisions

Line 460:
</lang>
 
'''Extra credit:'''
 
<lang lisp>(defun n-cartesian-product (l)
Line 473 ⟶ 474:
collect (cons x y)))))</lang>
 
'''Output:'''
Example of use:
 
<lang lisp>CL-USER> (n-cartesian-product '((11776 21789) (7 312) (304 14 23) (5000 1001)))
((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))
CL-USER> (n-cartesian-product '((1 2 3) () (500 100)))
NIL
</lang>
 
Anonymous user