Skip to content

Instantly share code, notes, and snippets.

@airencracken
Last active August 29, 2015 14:01
Show Gist options
  • Save airencracken/047c7d50ab8891c15ee7 to your computer and use it in GitHub Desktop.
Save airencracken/047c7d50ab8891c15ee7 to your computer and use it in GitHub Desktop.
Perl Factorial with GOTO TCO Hack
#!/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