Last active
August 29, 2015 14:01
-
-
Save airencracken/047c7d50ab8891c15ee7 to your computer and use it in GitHub Desktop.
Perl Factorial with GOTO TCO Hack
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/env perl | |
use 5.10.0; | |
use strict; | |
use warnings; | |
my $input = shift @ARGV || die "Give me a number\n"; | |
sub factorial { | |
my ($n, $total) = @_; | |
$total ||= 1; | |
return $total if $n == 0; | |
@_ = ($n - 1, $n * $total); | |
goto &factorial; | |
} | |
say factorial($input); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment