2017年3月17日金曜日

開発環境

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 4(Conditionals and recursion)のExercises 4-4-1、2.を取り組んでみる。

Exercises 4-4-1、2.

コード(Emacs)

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

say '1.';

my $a = 1;
my $b = 1;

for 1..20 {
    if $_ == 1 or $_ == 2 {
        say "F$_ = 1";
    } else {
        my $t = $a + $b;
        say "F$_ = ", $t;
        $a = $b;
        $b = $t;
    }
}

say '2.';

for 1..20 {
    my $n = $*IN.get;
    my $a = 1;
    my $b = 1;
    for 3..$n {
        my $t = $a + $b;
        $a = $b;
        $b = $t;
    }
    say "F$n = $b";
}

入出力結果(Terminal, REPL)

$ ./sample4.pl < sample4.txt 
1.
F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144
F13 = 233
F14 = 377
F15 = 610
F16 = 987
F17 = 1597
F18 = 2584
F19 = 4181
F20 = 6765
2.
F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144
F13 = 233
F14 = 377
F15 = 610
F16 = 987
F17 = 1597
F18 = 2584
F19 = 4181
F20 = 6765
$ cat sample4.txt 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$

0 コメント:

コメントを投稿

Comments on Google+: