Community
Enjoy!
(defun GetCartesianProduct ( li / s1 sr i1 ir)
(if (null li)
'()
(progn
(setq s1 (car li) sr (cdr li))
(if (null sr)
(mapcar 'list s1)
(if (null s1)
'()
(progn
(setq i1 (car s1) ir (cdr s1))
(append
(mapcar '(lambda (x) (cons i1 x)) (GetCartesianProduct sr))
(GetCartesianProduct (cons ir sr))
)
)
)
)
)
)
)
- Lanny
Another version of this:
(defun cartesian-product (input / result) ;; ((1 2)(a b)(x y)) -> ((1 a x) (1 a y)(1 b x)(1 b y)(2 a x) (2 a y)(2 b x)(2 b y)) (cond ((null input) nil) ((null (cdr input)) (mapcar 'list (car input))) (T (foreach item (car input) (foreach product (cartesian-product (cdr input)) (setq result (cons (cons item product) result)))) (reverse result))))
--