unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Thierry Volpiatto <thierry.volpiatto@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: All Possible Combinations
Date: Fri, 05 Jun 2009 17:49:13 +0200	[thread overview]
Message-ID: <87vdna7kau.fsf@tux.homenetwork> (raw)
In-Reply-To: h07sj7$efs$1@news.onet.pl

Marc Tfardy <tfardol@very-tfardol.com> writes:

> Nordlöw schrieb:
>> On Jun 3, 8:50 pm, Marc Tfardy <tfar...@very-tfardol.com> 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





      parent reply	other threads:[~2009-06-05 15:49 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-03  9:09 All Possible Combinations Nordlöw
2009-06-03  9:53 ` Pascal J. Bourguignon
2009-06-03 13:36   ` B. T. Raven
2009-06-03  9:55 ` Lennart Borgman
2009-06-03 18:50 ` Marc Tfardy
2009-06-04  6:08   ` Nordlöw
2009-06-04  7:20     ` Marc Tfardy
2009-06-04 16:25       ` B. T. Raven
2009-06-05 15:49       ` Thierry Volpiatto [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87vdna7kau.fsf@tux.homenetwork \
    --to=thierry.volpiatto@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).