perlgolf and LISP come to use

jonasbn on 2003-01-21T11:25:29

I am working on my Test framework and currently I am working on the scenario builder.

I needed to calculate the number of scenarios based on the combinations of parameter blocks, I could remember how to do this in a smart fashion, but I found my old LISP folder, which is following my develop directory.

And there it was, faculty.lsp, here is my Perl implementation:

sub faculty ($$) { my ($self, $i) = @_;

return 1 if ($i <= 1);

return $i *= $self->faculty($i - 1); }


After this I started working on some code to create scenario combos and I remembered the Perlgolf tournament on permutations, and I located one of my readable revisions of my code for the tournament here. This is the revised version for use with the framework.

sub permute { my @items = @{ $_[0] }; my @perms = @{ $_[1] }; my $p = $_[2]; unless (@items) { push(@{$p}, \@perms); } else { my(@newitems,@newperms,$i); foreach $i (0 .. $#items) { @newitems = @items; @newperms = @perms; unshift(@newperms, splice(@newitems, $i, 1)); permute([@newitems], [@newperms], $p); } } }

Heh, fun to see when old stuff you got on your harddrive can be put to use.


method

bart on 2003-01-21T12:33:51

I wonder why you wrote it as a method. OTOH, it's a bit annoying Perl makes such a huge distinction between methods and plain functions, for the function arguments.

Re:method

jonasbn on 2003-01-21T14:10:35

Well actually it is not necessary but since I was using it in a class...

I like it better just as a plain function myself.