開発環境
- 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)のPart 1(Starting with the basics)、Chapter 3(Functions)のExercises 3-1、2、3.を取り組んでみる。
Exercises 3-1、2、3.
コード(Emacs)
#!/usr/bin/env perl6
# -*- coding: utf-8 -*-
say '1.';
sub right-justify(Str $input-string) {
say ' ' x (70 - $input-string.chars) ~ $input-string;
}
right-justify('Larry Wall');
say '2-1.';
sub do-twice($code) {
$code();
$code();
}
sub greet {
say 'Hell World!';
}
do-twice(&greet);
say '2-2.';
sub do-twice1($code, $value) {
$code($value);
$code($value);
}
say '2-3.';
sub print-twice($value) {
say $value;
say $value;
}
say '2-4.';
do-twice1(&print-twice, 'Hello World!');
say '2-5.';
sub do-four($code, $value) {
do-twice1($code, $value);
do-twice1($code, $value);
}
do-four(&print-twice, 'Hello World!');
say '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, '+';
}
grid;
say '3-2.';
sub grid1 {
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, '|';
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, '+';
}
grid1;
入出力結果(Terminal, REPL)
$ ./sample1.pl
1.
Larry Wall
2-1.
Hell World!
Hell World!
2-2.
2-3.
2-4.
Hello World!
Hello World!
Hello World!
Hello World!
2-5.
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
3-1.
+----+----+
| | |
| | |
| | |
| | |
+----+----+
| | |
| | |
| | |
| | |
+----+----+
3-2.
+----+----+----+----+
| | | | |
| | | | |
| | | | |
| | | | |
+----+----+----+----+
| | | | |
| | | | |
| | | | |
| | | | |
+----+----+----+----+
| | | | |
| | | | |
| | | | |
| | | | |
+----+----+----+----+
| | | | |
| | | | |
| | | | |
| | | | |
+----+----+----+----+
$
0 コメント:
コメントを投稿