NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.
All the Perl that's Practical to Extract and Report
Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.
re: How would you write factorial? (Score:1)
I wouldn't call this golfing or obfuscation. It's another take on the classic recursive algorithm, and might not be interesting. Anyway, here's how I'd write the recursive version:
sub factorial {
my $v = shift;
return 1 if $v == 1;
return $v * factorial( $v - 1 );
}
I like to get the terminating case out of the way early on, and then read the meat of the algorithm (or soy of the algorithm, if you're inclined that way) afterward. I suspect I might be standing alone when I say that I find my version more readable for eliminating the if/then blocks, though.
I really liked your dynamic version of the algorithm, it's like the substitution model on steroids!
Cheers,
-- Douglas
Reply to This