#
# This software is Copyright 2005 by Elsevier Inc.  You may use it
# under the terms of the license at http://perl.plover.com/hop/LICENSE.txt .
#



###
### permute-flatten-imap.pl
###

## Chapter 4 section 6.6

sub permute {
  my @items = @{ $_[0] };
  my @perms = @{ $_[1] };
  unless (@items) {
    return \@perms;
  } else {
    flatten(imap {
      my(@newitems,@newperms);
      @newitems = @items;
      @newperms = @perms;
      unshift(@newperms, splice(@newitems, $_, 1));
      permute([@newitems], [@newperms]);
    } upto(0,$#items));
  }
}
