spurperl has asked for the wisdom of the Perl Monks concerning the following question:
Following my attempts to write elegant "Lispy" code in Perl I came upon the interesting algorithm of recursive generation of permutations. I.e. for (1 2 3) the permutations are (1 2 3) (1 3 2) (2 1 3) (2 3 1) (3 1 2) (3 2 1).
Like many problems, though at first seemingly difficult, recursion is a powerful technique for extremely elegant solutions.
All permutations of N elements can be expressed in terms of all permutations of N - 1 elements. Having a set of N elements, we take one of them and append it to all the permutations of the remaining N - 1 elements. Repeating it for every element of N gives us all permutations of N elements. Unsurprisingly, it fits well into the known formula of the amount of permutations of N elements: the factorial of N, N!.
This is expressed very succinctly and elegantly by the following Lisp code (8 lines without comments / documentation):
(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)))
Lisp's command of lists shines in this example, and replicating the code in Perl as succinctly (without sacrificing readability) is quite challenging. What I paste below is a straightforward implementation, without delving too much into succinctness.
First, lets define a couple of helper functions:
remove would look good if it were not for the argument collection. Named arguments with support for optional arguments that have default values would be of great help. Happily, it will be provided in Perl6, and this function will look much better.sub is_empty_list { return @{$_[0]} == 0; } # remove ($what, $list, $howmany) # # Removes $what from the list $list up to $howmany times. # If $howmany is not provided, removes all occurrences of $what. # sub remove { my $what = $_[0]; my $list = $_[1]; my $howmany = defined $_[2] ? $_[2] : -1; my @ret = (); foreach my $elem (@{$list}) { push @ret, $elem unless ($elem == $what and $howmany-- > 0); } return \@ret; }
Now, to the permutations function itself:
This could have been made much nicer if Perl had a sane way to write nested maps... Also, it seems to me that unshift and push would be much better understood as prepend and append.sub permutations { my $bag = $_[0]; if (is_empty_list($bag)) { return [[]]; } my $perms = []; foreach my $elem (@$bag) { foreach my $perm (@{permutations(remove($elem, $bag, 1))}) { unshift(@{$perm}, $elem); push(@{$perms}, $perm); } } return $perms; }
Any ideas/suggestion to make this code more elegant ?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: recursively generating permutations
by Roy Johnson (Monsignor) on Aug 19, 2005 at 11:29 UTC | |
by spurperl (Priest) on Aug 19, 2005 at 11:36 UTC | |
|
Re: recursively generating permutations
by tlm (Prior) on Aug 19, 2005 at 11:35 UTC | |
by Roy Johnson (Monsignor) on Aug 19, 2005 at 11:42 UTC | |
|
Re: recursively generating permutations
by rnahi (Curate) on Aug 19, 2005 at 11:20 UTC | |
by spurperl (Priest) on Aug 19, 2005 at 11:33 UTC | |
|
Re: recursively generating permutations
by Roger (Parson) on Aug 19, 2005 at 12:28 UTC | |
|
Re: recursively generating permutations
by tlm (Prior) on Aug 20, 2005 at 01:23 UTC | |
|
Re: recursively generating permutations
by BrowserUk (Patriarch) on Aug 20, 2005 at 05:05 UTC | |
|
Re: recursively generating permutations
by Anonymous Monk on May 12, 2008 at 04:27 UTC |