2017年5月26日金曜日

開発環境

Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)のPart 1(Starting with the basics)、Chapter 11(Case Study: Data Structure Selection)の Constructing New Operators、Exercise: Constructing New Operators.を取り組んでみる。

Exercise: Constructing New Operators.

コード(Emacs)

#!/usr/bin/env perl6
# -*- coding: utf-8 -*-

multi sub postfix:<!>(Int $n where $n >= 0) {
    sub inner($n, $res) {
        return $res if $n == 0;
        return inner($n - 1, $n * $res);
    }
    return inner($n, 1);
}

for 0..10 {
    say "$_! = ", $_!;
}
say "100! = ", 100!;

入出力結果(Terminal, REPL)

$ ./sample02.pl
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
$

0 コメント:

コメントを投稿