(defun cartesian-product (list &rest lists) (if (endp lists) (mapcar #'list list) (apply #'append (mapcar #'(lambda (head) (mapcar #'(lambda (tail) (cons head tail)) (apply #'cartesian-product lists))) list))))