From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Thierry Volpiatto Newsgroups: gmane.emacs.help Subject: Re: All Possible Combinations Date: Fri, 05 Jun 2009 17:49:13 +0200 Organization: ThierryVolpiatto Message-ID: <87vdna7kau.fsf@tux.homenetwork> References: <778c22e3-3233-4cec-899e-c9f77208155a@z14g2000yqa.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1244217393 22662 80.91.229.12 (5 Jun 2009 15:56:33 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 5 Jun 2009 15:56:33 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Jun 05 17:56:29 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 1MCbmO-000743-LS for geh-help-gnu-emacs@m.gmane.org; Fri, 05 Jun 2009 17:56:28 +0200 Original-Received: from localhost ([127.0.0.1]:59526 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MCbmO-0007Mj-1P for geh-help-gnu-emacs@m.gmane.org; Fri, 05 Jun 2009 11:56:28 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MCbm2-0007Ma-Qc for help-gnu-emacs@gnu.org; Fri, 05 Jun 2009 11:56:06 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MCbly-0007Im-20 for help-gnu-emacs@gnu.org; Fri, 05 Jun 2009 11:56:06 -0400 Original-Received: from [199.232.76.173] (port=49087 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MCblx-0007Ij-Ut for help-gnu-emacs@gnu.org; Fri, 05 Jun 2009 11:56:01 -0400 Original-Received: from main.gmane.org ([80.91.229.2]:45430 helo=ciao.gmane.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1MCblx-0005kR-C8 for help-gnu-emacs@gnu.org; Fri, 05 Jun 2009 11:56:01 -0400 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1MCbls-0004QI-8F for help-gnu-emacs@gnu.org; Fri, 05 Jun 2009 15:55:56 +0000 Original-Received: from 83.77.197-77.rev.gaoland.net ([77.197.77.83]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 05 Jun 2009 15:55:56 +0000 Original-Received: from thierry.volpiatto by 83.77.197-77.rev.gaoland.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 05 Jun 2009 15:55:56 +0000 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 80 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 83.77.197-77.rev.gaoland.net Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAElBMVEUHAwNfJRBVTESlTym0 RB/OwKFivhZqAAAACXBIWXMAAAsSAAALEgHS3X78AAAACXZwQWcAAAAwAAAAMADO7oxXAAABeElE QVQ4y3VTW47CMAyMwwUyTfYfUvG/wfC/ET4B0t7/KmsHurRgXFVVPRm/xglhsRj2+npW98G3+Amp tS5BXxl3SqzcXGRuEyf7n5lPA9jXetNPRhE7EJkzfz8YWnIgBSwW95TK4H79/t4GINKNADWj4PgA DiJX/ZxFDzDXeb4oYMw7kNUvyMzcaj3ioICG4hCKEShkPpwQ49Q05nnkGAQLDFDdExjm0qpowBYA Wi61bDlFU1GW0WbY2dSJYX5ZD2S0AiZL+rPyE19TpMzP2IvNWly+NkuWNgD4pO2oL73IQXoSyVX2 NHna7tgadWRXoCdvf2guqEcvGHqZwE520iRD0/QCYiiTE+U21w1Qxvg6iSp9fbIiDCnSAelDkCVF Vn+HekqXzRybwJ40xF4DMRG6SWGK9G1lYzHSxLYEm05GWbpJCB6A9tb9A3BGn2XdwJbS/WuInj74 4QNF8sVFdP3vl+5tgYp4Ev7fyrfFMkXgElDgV4uV/w8TWEeLCIGJ/AAAACV0RVh0Y3JlYXRlLWRh dGUAMjAwOC0wNS0zMVQyMjoxNDoyMiswMjowMM7uH/IAAAARdEVYdGpwZWc6Y29sb3JzcGFjZQAy LHVVnwAAACB0RVh0anBlZzpzYW1wbGluZy1mYWN0b3IAMngyLDF4MSwxeDFJ+qa0AAAAJXRFWHRt b2RpZnktZGF0ZQAyMDA3LTEwLTI4VDE3OjA4OjQ4KzAxOjAwhqNSRgAAAABJRU5ErkJggg== User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.94 (gnu/linux) Cancel-Lock: sha1:ZJXYccRLrA8QJaS44D7i/0Ppljo= X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) 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:64972 Archived-At: Marc Tfardy writes: > Nordlöw schrieb: >> On Jun 3, 8:50 pm, Marc Tfardy wrote: >>> 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 >> >> This gives the error: "e is undefined" in Emacs: >> >> (defun all-combinations-gen (bag) >> "Return a list of all the permutations of the input." >> (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))) >> >> (all-combinations-gen '(a b c)) > > Check all again, please. I've tested this code just now once again > and here is everything working fine. I get: > > > (all-combinations-gen '(a b c)) > ((a b c) (a c b) (b a c) (b c a) (c a b) (c b a)) Work fine for me too, very nice code :-) Would be nice if the same function accept an extra arg to specify the number of elements of the combinations we want, something like: (all-combinations-gen '(a b c) 2) ==> ((a b) (a c) (b c) ....) Don't find yet how to do that ... -- A + Thierry Volpiatto Location: Saint-Cyr-Sur-Mer - France