* All Possible Combinations
@ 2009-06-03 9:09 Nordlöw
2009-06-03 9:53 ` Pascal J. Bourguignon
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Nordlöw @ 2009-06-03 9:09 UTC (permalink / raw)
To: help-gnu-emacs
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.
Thanks in advance,
Nordlöw
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: All Possible Combinations
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
2 siblings, 1 reply; 9+ messages in thread
From: Pascal J. Bourguignon @ 2009-06-03 9:53 UTC (permalink / raw)
To: help-gnu-emacs
Nordlöw <per.nordlow@gmail.com> writes:
> 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.
It's called permutations. Combinations are when you take a smaller
number of elements amongst the set.
Now notice that (permutations '()) = (())
that is, the set of permutations of the empty set contains only the
empty set (there's no other way to order no elements)
and notice that (permutations '(a)) = ((a))
(there's only one way to order one element).
Now, knowing that (permutations '(a)) = ((a))
How can you compute (permutations '(b a))?
That is, how many ways can you put b in the permutations of (a), for example, in (a)?
Yes, there's two ways: before or after a: (b a) or (a b).
So now we know that (permutations '(b a)) = ((b a) (a b))
Then, how can you compute (permutations '(c b a))?
That is, how many ways can you put c in the permutations of (b a), for example, in (a b)?
...
So now we know that (permutations '(x ...)) = ((x ...) ... (... x))
Then, how can you compute (permutations '(y x ...))?
That is, how many ways can you put y in the permutations of (x ...), for example, in (... x)?
--
__Pascal Bourguignon__
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: All Possible Combinations
2009-06-03 9:09 All Possible Combinations Nordlöw
2009-06-03 9:53 ` Pascal J. Bourguignon
@ 2009-06-03 9:55 ` Lennart Borgman
2009-06-03 18:50 ` Marc Tfardy
2 siblings, 0 replies; 9+ messages in thread
From: Lennart Borgman @ 2009-06-03 9:55 UTC (permalink / raw)
To: Nordlöw; +Cc: help-gnu-emacs
On Wed, Jun 3, 2009 at 11:09 AM, Nordlöw <per.nordlow@gmail.com> wrote:
> 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.
Would not that require building a stack to keep track of state just as
recursion would do internally?
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: All Possible Combinations
2009-06-03 9:53 ` Pascal J. Bourguignon
@ 2009-06-03 13:36 ` B. T. Raven
0 siblings, 0 replies; 9+ messages in thread
From: B. T. Raven @ 2009-06-03 13:36 UTC (permalink / raw)
To: help-gnu-emacs
Pascal J. Bourguignon wrote:
> Nordlöw <per.nordlow@gmail.com> writes:
>
>> 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.
>
> It's called permutations. Combinations are when you take a smaller
> number of elements amongst the set.
Both permutations and combinations can deal with n things taken r at a
time where r can equal n:
nPr = n! / (n-r)!
nCr = n! / (n-r)! r!
So in general (at least when r /= 1) the number of resultant sets is
smaller with nCr
>
> Now notice that (permutations '()) = (())
> that is, the set of permutations of the empty set contains only the
> empty set (there's no other way to order no elements)
>
> and notice that (permutations '(a)) = ((a))
> (there's only one way to order one element).
>
> Now, knowing that (permutations '(a)) = ((a))
> How can you compute (permutations '(b a))?
> That is, how many ways can you put b in the permutations of (a), for example, in (a)?
>
> Yes, there's two ways: before or after a: (b a) or (a b).
>
>
> So now we know that (permutations '(b a)) = ((b a) (a b))
> Then, how can you compute (permutations '(c b a))?
> That is, how many ways can you put c in the permutations of (b a), for example, in (a b)?
>
> ...
>
> So now we know that (permutations '(x ...)) = ((x ...) ... (... x))
> Then, how can you compute (permutations '(y x ...))?
> That is, how many ways can you put y in the permutations of (x ...), for example, in (... x)?
>
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: All Possible Combinations
2009-06-03 9:09 All Possible Combinations Nordlöw
2009-06-03 9:53 ` Pascal J. Bourguignon
2009-06-03 9:55 ` Lennart Borgman
@ 2009-06-03 18:50 ` Marc Tfardy
2009-06-04 6:08 ` Nordlöw
2 siblings, 1 reply; 9+ messages in thread
From: Marc Tfardy @ 2009-06-03 18:50 UTC (permalink / raw)
To: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: All Possible Combinations
2009-06-03 18:50 ` Marc Tfardy
@ 2009-06-04 6:08 ` Nordlöw
2009-06-04 7:20 ` Marc Tfardy
0 siblings, 1 reply; 9+ messages in thread
From: Nordlöw @ 2009-06-04 6:08 UTC (permalink / raw)
To: help-gnu-emacs
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))
/Per
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: All Possible Combinations
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
0 siblings, 2 replies; 9+ messages in thread
From: Marc Tfardy @ 2009-06-04 7:20 UTC (permalink / raw)
To: help-gnu-emacs
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))
regards
Marc
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: All Possible Combinations
2009-06-04 7:20 ` Marc Tfardy
@ 2009-06-04 16:25 ` B. T. Raven
2009-06-05 15:49 ` Thierry Volpiatto
1 sibling, 0 replies; 9+ messages in thread
From: B. T. Raven @ 2009-06-04 16:25 UTC (permalink / raw)
To: help-gnu-emacs
Marc Tfardy wrote:
> 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))
>
> regards
> Marc
>
Also doesn't quite work here:
Backtrace:
Debugger entered--Lisp error: (void-function permutations)
(permutations (remove* e bag :count 1))
(mapcar (function (lambda ... ...)) (permutations (remove* e bag
:count 1)))
...
I'm assuming that all I need is M-x load-library cl to get mapcan and
remove*
I tried on ver 22 but not 23
Okay, finally comes the light: defun has to be named either
all-combinations-gen or permutations. Works if that renaming is made
consistent. Should be permutations, since there is only one combination
of elements a,b,c taken three at a time. All permutations collapse into
one combination.
Ed
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: All Possible Combinations
2009-06-04 7:20 ` Marc Tfardy
2009-06-04 16:25 ` B. T. Raven
@ 2009-06-05 15:49 ` Thierry Volpiatto
1 sibling, 0 replies; 9+ messages in thread
From: Thierry Volpiatto @ 2009-06-05 15:49 UTC (permalink / raw)
To: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2009-06-05 15:49 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.