Help us understand the problem. What is going on with this article?

順列列挙(再帰版)

More than 5 years have passed since last update.

以前書いたのはSTLをコピーした反復版だったが、再帰版は以下。Perlはシンプル。

permutation.pl
sub permutation {
    my ($first, @rest) = @_; 
    @rest or return [[@_]];

    my @result;
    for (@_) {
        push @rest, $first;
        $first = shift @rest;
        push @result , [$first, @$_]
            for @{permutation(@rest)};
    }   
    return [@result];
}

print @$_ , "\n"
    for @{permutation(1..4)};
2431
2413
2143
2134
2314
2341
3142
3124
3214
3241
3421
3412
4213
4231
4321
4312
4132
4123
1324
1342
1432
1423
1243
1234
satzz
Web Engineer 2009-2011 Hatena(part-time) 2011-2015 DeNA
http://satzz.me
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away