in reply to recursively generating permutations

I don't know how much of an improvement this is; at least it doesn't use helper functions, for what that's worth:

sub perms { my $arr = shift; if ( !@$arr ) { return ( [] ); } else { return map { my @a = @$arr; my $e = splice @a, $_, 1; map [ $e, @$_ ], perms( \@a ) } ( 0..$#$arr ); } }

Update: I changed the original to use nested maps (my original attempt to do this had a pesky bug, so I posted the working for + push version). I see that Roy Johnson had the same idea.

the lowliest monk

Replies are listed 'Best First'.
Re^2: recursively generating permutations
by Roy Johnson (Monsignor) on Aug 19, 2005 at 11:42 UTC
    Mapifying the else block (untested):
    else { return map { my @a = @$arr; my $e = splice @a, $_, 1; map [ $e, @$_ ], perms( \@a ); } 0..$#$arr; }

    Caution: Contents may have been coded under pressure.