Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!yale!Duchier-Denys From: Duchier-Denys@cs.yale.edu (Denys Duchier) Newsgroups: comp.lang.lisp Subject: Re: Cartesian product Message-ID: <56828@yale-celray.yale.UUCP> Date: 13 Apr 89 02:30:40 GMT References: <3677@sdsu.UUCP> Sender: root@yale.UUCP Reply-To: Duchier-Denys@cs.yale.edu (Denys Duchier) Organization: Computer Science, Yale University, New Haven, CT 06520-2158 Lines: 15 In-reply-to: ucselx!maxc0186@sdsu In article <3677@sdsu.UUCP>, ucselx!maxc0186@sdsu writes: > Does anyone have a lisp function that performs the Cartesian product > on a list, i.e., > (CARTESIAN '((A B) (C D) (E F))) will return > ((A C E) (A C F) (A D E) (A D F) (B C E) (B C F) (B D E) (B D F)) > not necessarily in that order. (DEFUN CARTESIAN (L) (COND ((NULL L) NIL) ((NULL (CDR L)) (MAPCAR #'LIST (CAR L))) (T (MAPCAN #'(LAMBDA (X) (MAPCAR #'(LAMBDA (Y) (CONS Y X)) (CAR L))) (CARTESIAN (CDR L)))))) --Denys