2017年2月24日金曜日

開発環境

Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)のChapter 5(Fruitful subroutines)のExercises 5-1、2、3、4、5.を取り組んでみる。

Exercises 5-1、2、3、4、5.

コード(Emacs)

#!/usr/bin/env perl6

# 5-1.
# 8100

# 5-2.
sub ack($m, $n) {
    return $n + 1 if $m == 0;
    return ack($m - 1, 1) if $m > 0 and $n == 0;
    ack($m - 1, ack($m, $n - 1));
}

say ack(3, 4);

# 5-3.
sub first_l(Str $word) {
    substr $word, 0, 1;
}
sub last_l(Str $word) {
    substr $word, *-1, 1;
}
sub middle_l(Str $word) {
    substr $word, 1, *-1;
}
sub is-palindrome($word) {
    return True if $word.chars <= 1;
    first_l($word) eq last_l($word) and is-palindrome(middle_l($word));
}
say is-palindrome('');
say is-palindrome('a');
say is-palindrome('noon');
say is-palindrome('redivider');
say is-palindrome('perl 6');

# 5-4.
sub is-power-of ($a, $b) {
    return False if $b == 1 and $a != 1;
    return True if $a == 1;
    $a %% $b and is-power-of($a / $b, $b);
}

for 1..10 -> $a {
    for 1..10 -> $b {
        if is-power-of $a, $b {
            say "$a is power of $b";
        } else {
            say "$a is not power of $b";
        }
    }
}

# 5-5.
sub gcd($a, $b) {
    return $a if $b == 0;
    gcd($b, $a % $b);
}

for 1..10 -> $a {
    for 1..10 -> $b {
        say "gcd($a, $b) => ", gcd($a, $b);
    }
}

入出力結果(Terminal)

$ ./sample1.pl
125
True
True
True
True
False
1 is power of 1
1 is power of 2
1 is power of 3
1 is power of 4
1 is power of 5
1 is power of 6
1 is power of 7
1 is power of 8
1 is power of 9
1 is power of 10
2 is not power of 1
2 is power of 2
2 is not power of 3
2 is not power of 4
2 is not power of 5
2 is not power of 6
2 is not power of 7
2 is not power of 8
2 is not power of 9
2 is not power of 10
3 is not power of 1
3 is not power of 2
3 is power of 3
3 is not power of 4
3 is not power of 5
3 is not power of 6
3 is not power of 7
3 is not power of 8
3 is not power of 9
3 is not power of 10
4 is not power of 1
4 is power of 2
4 is not power of 3
4 is power of 4
4 is not power of 5
4 is not power of 6
4 is not power of 7
4 is not power of 8
4 is not power of 9
4 is not power of 10
5 is not power of 1
5 is not power of 2
5 is not power of 3
5 is not power of 4
5 is power of 5
5 is not power of 6
5 is not power of 7
5 is not power of 8
5 is not power of 9
5 is not power of 10
6 is not power of 1
6 is not power of 2
6 is not power of 3
6 is not power of 4
6 is not power of 5
6 is power of 6
6 is not power of 7
6 is not power of 8
6 is not power of 9
6 is not power of 10
7 is not power of 1
7 is not power of 2
7 is not power of 3
7 is not power of 4
7 is not power of 5
7 is not power of 6
7 is power of 7
7 is not power of 8
7 is not power of 9
7 is not power of 10
8 is not power of 1
8 is power of 2
8 is not power of 3
8 is not power of 4
8 is not power of 5
8 is not power of 6
8 is not power of 7
8 is power of 8
8 is not power of 9
8 is not power of 10
9 is not power of 1
9 is not power of 2
9 is power of 3
9 is not power of 4
9 is not power of 5
9 is not power of 6
9 is not power of 7
9 is not power of 8
9 is power of 9
9 is not power of 10
10 is not power of 1
10 is not power of 2
10 is not power of 3
10 is not power of 4
10 is not power of 5
10 is not power of 6
10 is not power of 7
10 is not power of 8
10 is not power of 9
10 is power of 10
gcd(1, 1) => 1
gcd(1, 2) => 1
gcd(1, 3) => 1
gcd(1, 4) => 1
gcd(1, 5) => 1
gcd(1, 6) => 1
gcd(1, 7) => 1
gcd(1, 8) => 1
gcd(1, 9) => 1
gcd(1, 10) => 1
gcd(2, 1) => 1
gcd(2, 2) => 2
gcd(2, 3) => 1
gcd(2, 4) => 2
gcd(2, 5) => 1
gcd(2, 6) => 2
gcd(2, 7) => 1
gcd(2, 8) => 2
gcd(2, 9) => 1
gcd(2, 10) => 2
gcd(3, 1) => 1
gcd(3, 2) => 1
gcd(3, 3) => 3
gcd(3, 4) => 1
gcd(3, 5) => 1
gcd(3, 6) => 3
gcd(3, 7) => 1
gcd(3, 8) => 1
gcd(3, 9) => 3
gcd(3, 10) => 1
gcd(4, 1) => 1
gcd(4, 2) => 2
gcd(4, 3) => 1
gcd(4, 4) => 4
gcd(4, 5) => 1
gcd(4, 6) => 2
gcd(4, 7) => 1
gcd(4, 8) => 4
gcd(4, 9) => 1
gcd(4, 10) => 2
gcd(5, 1) => 1
gcd(5, 2) => 1
gcd(5, 3) => 1
gcd(5, 4) => 1
gcd(5, 5) => 5
gcd(5, 6) => 1
gcd(5, 7) => 1
gcd(5, 8) => 1
gcd(5, 9) => 1
gcd(5, 10) => 5
gcd(6, 1) => 1
gcd(6, 2) => 2
gcd(6, 3) => 3
gcd(6, 4) => 2
gcd(6, 5) => 1
gcd(6, 6) => 6
gcd(6, 7) => 1
gcd(6, 8) => 2
gcd(6, 9) => 3
gcd(6, 10) => 2
gcd(7, 1) => 1
gcd(7, 2) => 1
gcd(7, 3) => 1
gcd(7, 4) => 1
gcd(7, 5) => 1
gcd(7, 6) => 1
gcd(7, 7) => 7
gcd(7, 8) => 1
gcd(7, 9) => 1
gcd(7, 10) => 1
gcd(8, 1) => 1
gcd(8, 2) => 2
gcd(8, 3) => 1
gcd(8, 4) => 4
gcd(8, 5) => 1
gcd(8, 6) => 2
gcd(8, 7) => 1
gcd(8, 8) => 8
gcd(8, 9) => 1
gcd(8, 10) => 2
gcd(9, 1) => 1
gcd(9, 2) => 1
gcd(9, 3) => 3
gcd(9, 4) => 1
gcd(9, 5) => 1
gcd(9, 6) => 3
gcd(9, 7) => 1
gcd(9, 8) => 1
gcd(9, 9) => 9
gcd(9, 10) => 1
gcd(10, 1) => 1
gcd(10, 2) => 2
gcd(10, 3) => 1
gcd(10, 4) => 2
gcd(10, 5) => 5
gcd(10, 6) => 2
gcd(10, 7) => 1
gcd(10, 8) => 2
gcd(10, 9) => 1
gcd(10, 10) => 10
$

0 コメント:

コメントを投稿