Skip to content

Instantly share code, notes, and snippets.

@marcoonroad
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcoonroad/0bdda6e3d8d77a14388d to your computer and use it in GitHub Desktop.
Save marcoonroad/0bdda6e3d8d77a14388d to your computer and use it in GitHub Desktop.
Perl 6 way of Factorial function.
#!/usr/bin/perl6
use v6;
# overloading string 'x' operator
multi infix:<x> (Int \a, Int \b) { a * b }
# but we still want the string 'x' operator
multi infix:<x> (Str \a, Int \b where * > 1) { [~] gather for ^b { take a } }
multi postfix:<!> (1) { 1 }
multi postfix:<!> (Int \n where * > 1) { n x (n - 1)! }
say 5!; # 120
# end of script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment