Last active
August 29, 2015 14:07
-
-
Save marcoonroad/0bdda6e3d8d77a14388d to your computer and use it in GitHub Desktop.
Perl 6 way of Factorial function.
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/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