Created
April 13, 2010 08:56
-
-
Save hryk/364445 to your computer and use it in GitHub Desktop.
順列 perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
my $main = sub { | |
my $h = shift; | |
print $h."\n"; | |
}; | |
my @ascii = qw/2 1/; | |
my $length = 4; | |
nise_permutate(\@ascii, $main, $length); | |
sub nise_permutate { | |
my ($set, $subref, $length) = @_; | |
my $nestfor = sub { nestfor($set, $_[0], $_[1]); }; | |
if ($length != 0) { $subref = gen($subref) for 1..$length; } | |
$nestfor->($subref, ''); | |
} | |
sub gen { | |
my $s = shift; | |
return sub {nestfor(\@ascii,$s,shift)}; | |
} | |
sub nestfor { | |
my $ary = shift; | |
my $sub = shift; | |
my $bind = shift || ''; | |
$sub->("$bind$_") for @$ary; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
順列