開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Perl 6 (プログラミング言語)
- Rakudo(コンパイラ、実装)
Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)のChapter 3(Functions)のExercises 3-1、2、3.を取り組んでみる。
Exercises 3-1、2、3.
コード(Emacs)
#!/usr/bin/env perl6
# 3-1.
sub right-justify($input-string) {
say ' ' x (70 - $input-string.chars;), $input-string;
}
right-justify('Larry Wall');
# 3-2.
# 2.
sub do-twice($code, $value) {
$code($value);
$code($value);
}
sub greet($value) {
say "Hello, $value!";
}
do-twice(&greet, 'world!');
# 4.
sub do-four($code, $value) {
do-twice($code, $value);
do-twice($code, $value);
}
do-four(&greet, 'Perl 6');
# 3-3.
# 1.
sub grid {
say '+', '-' x 4, '+', '-' x 4, '+';
say '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|';
say '+', '-' x 4, '+', '-' x 4, '+';
say '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|';
say '+', '-' x 4, '+', '-' x 4, '+';
}
grid;
# 2.
sub grid-four {
say '+', '-' x 4, '+', '-' x 4, '+', '-' x 4, '+', '-' x 4, '+';
say '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|';
say '+', '-' x 4, '+', '-' x 4, '+', '-' x 4, '+', '-' x 4, '+';
say '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|';
say '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|', ' ' x 4, '|';
say '+', '-' x 4, '+', '-' x 4, '+', '-' x 4, '+', '-' x 4, '+';
}
grid-four;
入出力結果(Terminal)
$ ./sample1.pl
Larry Wall
Hello, world!!
Hello, world!!
Hello, Perl 6!
Hello, Perl 6!
Hello, Perl 6!
Hello, Perl 6!
+----+----+
| | |
| | |
| | |
| | |
| | |
+----+----+
| | |
| | |
| | |
| | |
| | |
+----+----+
+----+----+----+----+
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
+----+----+----+----+
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
+----+----+----+----+
$
0 コメント:
コメントを投稿