Permutation Generator
This little program uses currying to create a nested function reference that prints out a table with all possible permutations of the @states
array in as many columns as you like.
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
## Scott Wiersdorf
## Created: Sat Aug 18 14:29:03 MDT 2012
## permutation/truth table generator
my $inputs = shift @ARGV || 3; ## table columns
my @states = ('T', 'F', '-'); ## possible states
my $func = sub { say join "\t" => @_ };
for (1..$inputs) {
$func = loop_maker($func);
}
$func->();
exit;
sub loop_maker {
my $inner = shift;
return sub {
for my $state ( @states ) {
$inner->(@_, $state);
}
};
}
The output with an argument of ‘2’ looks like:
while an argument of ‘3’ yields:
Last modified on 2012-08-24