From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Marc Tfardy Newsgroups: gmane.emacs.help Subject: Re: All Possible Combinations Date: Wed, 03 Jun 2009 20:50:00 +0200 Organization: http://onet.pl Message-ID: References: <778c22e3-3233-4cec-899e-c9f77208155a@z14g2000yqa.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1244085887 11121 80.91.229.12 (4 Jun 2009 03:24:47 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 4 Jun 2009 03:24:47 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Jun 04 05:24:42 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1MC3ZJ-0006aO-PJ for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Jun 2009 05:24:42 +0200 Original-Received: from localhost ([127.0.0.1]:49670 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MC3ZJ-0004kl-4s for geh-help-gnu-emacs@m.gmane.org; Wed, 03 Jun 2009 23:24:41 -0400 Original-Path: news.stanford.edu!headwall.stanford.edu!news.glorb.com!news2.glorb.com!news.unit0.net!news.nask.pl!news.nask.org.pl!news.onet.pl!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 44 Original-NNTP-Posting-Host: p4fd3478e.dip.t-dialin.net Original-X-Trace: news.onet.pl 1244055001 13477 79.211.71.142 (3 Jun 2009 18:50:01 GMT) Original-X-Complaints-To: niusy@onet.pl Original-NNTP-Posting-Date: Wed, 3 Jun 2009 18:50:01 +0000 (UTC) User-Agent: Thunderbird 2.0.0.21 (Windows/20090302) In-Reply-To: <778c22e3-3233-4cec-899e-c9f77208155a@z14g2000yqa.googlegroups.com> Original-Xref: news.stanford.edu gnu.emacs.help:169701 X-Mailman-Approved-At: Wed, 03 Jun 2009 23:23:25 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:64937 Archived-At: Nordlöw schrieb: > Hey! > > I want a function that generates all possible combinations (ordering) > of the elements in a list (or sequence if possible). Here is my > mockup: > > (defun all-combinations (n) > "Generate a listing of all the possible combinations of the > elements in the sequence N. Time-Complexity is N!" > (let (all) > all)) > > For example (all-combinations '(a b c)) should return '((a b c) (a c > b) (b a c) (b c a) (c a b) (c b a)) > > Has somebody written such a function, preferrably in an iterative > rather than recursive way. Here you find working common lisp version: http://www.perlmonks.org/?node_id=485066 (defun permutations (bag) "Return a list of all the permutations of the input." ;; If the input is nil, there is only one permutation: ;; nil itself (if (null bag) '(()) ;; Otherwise, take an element, e, out of the bag. ;; Generate all permutations of the remaining elements, ;; And add e to the front of each of these. ;; Do this for all possible e to generate all permutations. (mapcan #'(lambda (e) (mapcar #'(lambda (p) (cons e p)) (permutations (remove e bag :count 1)))) bag))) Now replace 'remove' with 'remove*' and this works in emacs lisp, too. regards Marc