Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Created January 31, 2010 09:01
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save miyagawa/290978 to your computer and use it in GitHub Desktop.
# Newbie programmer
$input=<>;
sub factorial {
$s=1;
$r=1;
while ($s <= $input) {
$r *= $s;
$s++;
}
if($input == 0)
{
$r=0;
}
return $r;
}
print "The result is ".&factorial;
# First year programmer, studied Pascal
sub factorial {
my $x = shift;
my $result = 1;
my $i = 2;
while ($i <= $x) {
$result = $result * $i;
$i = $i + 1;
}
return $result;
}
print factorial(6);
# First year programmer, studied C
sub fact($){
my $x = int(shift);
my $result = 1;
for (my $i = 1; $i <= $x; $i++) {
$result *= $i;
}
return $result;
}
print(fact(6));
# First year programmer, SICP
sub fact {
my($x, $acc) = @_;
if ($x > 1) { (return (fact(($x - 1), (($acc || 1) * $x)))) }
else { (return ($acc)) }
}
(print(fact(6)))
# First year programmer, Perl
sub factorial {
my $x = shift;
my $res = 1;
$res *= $_ for 2..$x;
return $res;
}
print factorial(6)
# Lazy Perl programmer
sub fact {
return $_[0] > 1 ? $_[0] * fact($_[0] - 1) : 1;
}
print fact(6);
# Lazier Perl programmer
sub fact { eval join '*', 1..$_[0] }
print fact(6);
# Expert Perl programmer
use List::Util 'reduce';
sub factorial {
reduce {$a*$b} 1..$_[0];
}
print factorial(6);
# Perl Hacker (who knows the other goto)
sub factorial {
my($n, $accum) = (shift, shift || 1);
if ($n == 1) {
return $accum;
} else {
@_ = ($n - 1, $n * $accum);
goto &factorial;
}
}
print factorial(6);
# Another Perl Hacker
use Sub::Call::Tail;
sub fact {
my($n, $accum) = (shift, shift || 1);
return $accum if $n == 1;
tail fact($n - 1, $accum * $n);
}
print fact(6);
# Clojure programmer
use Sub::Call::Recur;
sub fact {
my($n, $accum) = (shift, shift || 1);
return $accum if $n == 1;
recur($n - 1, $n * $accum);
}
print fact(6);
# search.cpan.org expert
use Math::Big qw(factorial);
print factorial(6);
# Academic programmer
use Math::Pari qw(factorial);
print factorial(6);
# Unix programmer
sub fact {
`factorial $_[0]`;
}
print fact(6);
# Regexp programmer
sub factorial {
local $_ = shift;
s/(?{$_=sub{$_[()]?($_[()]*$_->($_[()]-!())):@_}})(.*)/$_->($1)/e;
return $_;
}
print factorial(6);
# CGI programmer
sub fact {
local $q = $ENV{QUERY_STRING};
$q =~ s/%([0-9a-fA-F]{2})/chr(hex($1))/eg;
local %q = map { split /=/, $_ } split /[&;]/, $q;
my $x = $q{x} or &printErrorHTML("Bad input");
print STDERR "x=$x\n";
my $result = 1;
$result *= $_ for 2..$x;
print <<HTML;
Content-Type: text/html
Factorial of $x is $result.
HTML
}
# Haskell Progammer
use Language::Functional 'foldl';
sub fact {
my $x = shift;
foldl { $_[0] * $_[1] } 1, [ 2..$x ];
}
print fact(6);
# XS programmer
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
int fact(int n) { return n==0 ? 1 : n * fact(n-1); }
MODULE = Factorial PACKAGE = Factorial
PROTOTYPES: disable
IV
fact(int n)
# Real C programmer
use Inline C => <<'...';
int fact(int n) { return n==0 ? 1 : n * fact(n-1); }
...
print fact(6);
# Moose programmer
use MooseX::Declare;
class MooseX::Math {
method factorial ($class: Int $x) {
return $x > 1 ? $x * $class->factorial($x - 1) : 1;
}
}
print MooseX::Math->factorial(6);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment