2017年4月28日金曜日

開発環境

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 5(Fruitful Subroutines)の Exercises.を取り組んでみる。

Exercises.

コード(Emacs)

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

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

for 1..3 -> $m {
    for 1..4 -> $n {
        say "ack($m, $n) => ", ack($m, $n);
    }
}

say '3-1.';
sub first_l(Str $word) {
    return substr $word, 0, 1;
}
sub last_l(Str $word) {
    return substr $word, *-1, 1;
}
sub middle_l(Str $word) {
    return substr $word, 1, *-1;
}

say '3-2.';
say middle_l('ab');
# say middle_l('a');
# say middle_l('');

sub middle_l1(Str $word where $word.chars > 1) {
    return substr $word, 1, *-1;
}

sub is-palindrome(Str $word) {
    return True if $word.chars <= 1;
    return is-palindrome middle_l1($word) if first_l($word) eq last_l($word);
    False;
}

say is-palindrome '';
say is-palindrome 'a';
say not is-palindrome 'ab';
say is-palindrome 'aba';
say not is-palindrome 'abc';
say is-palindrome 'abba';
say not is-palindrome 'abca';


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

for 1..20 -> $a {
    for 2..3 -> $b {
        if is-power-of($a, $b) {
            say "$a is power of $b";
        } else {
            say "$a is not power of $b";
        }
    }
}

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

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

入出力結果(Terminal, REPL)

$ ./sample1.pl
2.
ack(1, 1) => 3
ack(1, 2) => 4
ack(1, 3) => 5
ack(1, 4) => 6
ack(2, 1) => 5
ack(2, 2) => 7
ack(2, 3) => 9
ack(2, 4) => 11
ack(3, 1) => 13
ack(3, 2) => 29
ack(3, 3) => 61
ack(3, 4) => 125
3-1.
3-2.

True
True
True
True
True
True
True
4.
1 is power of 2
1 is power of 3
2 is power of 2
2 is not power of 3
3 is not power of 2
3 is power of 3
4 is power of 2
4 is not power of 3
5 is not power of 2
5 is not power of 3
6 is power of 2
6 is power of 3
7 is not power of 2
7 is not power of 3
8 is power of 2
8 is not power of 3
9 is not power of 2
9 is power of 3
10 is power of 2
10 is not power of 3
11 is not power of 2
11 is not power of 3
12 is power of 2
12 is power of 3
13 is not power of 2
13 is not power of 3
14 is power of 2
14 is not power of 3
15 is not power of 2
15 is power of 3
16 is power of 2
16 is not power of 3
17 is not power of 2
17 is not power of 3
18 is power of 2
18 is power of 3
19 is not power of 2
19 is not power of 3
20 is power of 2
20 is not power of 3
5.
(2, 2) = 2
(2, 3) = 1
(2, 4) = 2
(2, 5) = 1
(2, 6) = 2
(2, 7) = 1
(2, 8) = 2
(2, 9) = 1
(2, 10) = 2
(3, 2) = 1
(3, 3) = 3
(3, 4) = 1
(3, 5) = 1
(3, 6) = 3
(3, 7) = 1
(3, 8) = 1
(3, 9) = 3
(3, 10) = 1
(4, 2) = 2
(4, 3) = 1
(4, 4) = 4
(4, 5) = 1
(4, 6) = 2
(4, 7) = 1
(4, 8) = 4
(4, 9) = 1
(4, 10) = 2
(5, 2) = 1
(5, 3) = 1
(5, 4) = 1
(5, 5) = 5
(5, 6) = 1
(5, 7) = 1
(5, 8) = 1
(5, 9) = 1
(5, 10) = 5
(6, 2) = 2
(6, 3) = 3
(6, 4) = 2
(6, 5) = 1
(6, 6) = 6
(6, 7) = 1
(6, 8) = 2
(6, 9) = 3
(6, 10) = 2
(7, 2) = 1
(7, 3) = 1
(7, 4) = 1
(7, 5) = 1
(7, 6) = 1
(7, 7) = 7
(7, 8) = 1
(7, 9) = 1
(7, 10) = 1
(8, 2) = 2
(8, 3) = 1
(8, 4) = 4
(8, 5) = 1
(8, 6) = 2
(8, 7) = 1
(8, 8) = 8
(8, 9) = 1
(8, 10) = 2
(9, 2) = 1
(9, 3) = 3
(9, 4) = 1
(9, 5) = 1
(9, 6) = 3
(9, 7) = 1
(9, 8) = 1
(9, 9) = 9
(9, 10) = 1
(10, 2) = 2
(10, 3) = 1
(10, 4) = 2
(10, 5) = 5
(10, 6) = 2
(10, 7) = 1
(10, 8) = 2
(10, 9) = 1
(10, 10) = 10
$

0 コメント:

コメントを投稿