開発環境
- 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 5(Fruitful Subroutines)のExercises(Compare).を取り組んでみる。
Exercises(Compare).
コード(Emacs)
#!/usr/bin/env perl6 # -*- coding: utf-8 -*- sub compare($x, $y) { if $x > $y { return 1; } if ($x == $y) { return 0; } return -1; } for 1..10 -> $x { for 1..10 -> $y { say "compare($x, $y) => ", compare($x, $y); } }
入出力結果(Terminal, REPL)
$ ./sample_compare.pl compare(1, 1) => 0 compare(1, 2) => -1 compare(1, 3) => -1 compare(1, 4) => -1 compare(1, 5) => -1 compare(1, 6) => -1 compare(1, 7) => -1 compare(1, 8) => -1 compare(1, 9) => -1 compare(1, 10) => -1 compare(2, 1) => 1 compare(2, 2) => 0 compare(2, 3) => -1 compare(2, 4) => -1 compare(2, 5) => -1 compare(2, 6) => -1 compare(2, 7) => -1 compare(2, 8) => -1 compare(2, 9) => -1 compare(2, 10) => -1 compare(3, 1) => 1 compare(3, 2) => 1 compare(3, 3) => 0 compare(3, 4) => -1 compare(3, 5) => -1 compare(3, 6) => -1 compare(3, 7) => -1 compare(3, 8) => -1 compare(3, 9) => -1 compare(3, 10) => -1 compare(4, 1) => 1 compare(4, 2) => 1 compare(4, 3) => 1 compare(4, 4) => 0 compare(4, 5) => -1 compare(4, 6) => -1 compare(4, 7) => -1 compare(4, 8) => -1 compare(4, 9) => -1 compare(4, 10) => -1 compare(5, 1) => 1 compare(5, 2) => 1 compare(5, 3) => 1 compare(5, 4) => 1 compare(5, 5) => 0 compare(5, 6) => -1 compare(5, 7) => -1 compare(5, 8) => -1 compare(5, 9) => -1 compare(5, 10) => -1 compare(6, 1) => 1 compare(6, 2) => 1 compare(6, 3) => 1 compare(6, 4) => 1 compare(6, 5) => 1 compare(6, 6) => 0 compare(6, 7) => -1 compare(6, 8) => -1 compare(6, 9) => -1 compare(6, 10) => -1 compare(7, 1) => 1 compare(7, 2) => 1 compare(7, 3) => 1 compare(7, 4) => 1 compare(7, 5) => 1 compare(7, 6) => 1 compare(7, 7) => 0 compare(7, 8) => -1 compare(7, 9) => -1 compare(7, 10) => -1 compare(8, 1) => 1 compare(8, 2) => 1 compare(8, 3) => 1 compare(8, 4) => 1 compare(8, 5) => 1 compare(8, 6) => 1 compare(8, 7) => 1 compare(8, 8) => 0 compare(8, 9) => -1 compare(8, 10) => -1 compare(9, 1) => 1 compare(9, 2) => 1 compare(9, 3) => 1 compare(9, 4) => 1 compare(9, 5) => 1 compare(9, 6) => 1 compare(9, 7) => 1 compare(9, 8) => 1 compare(9, 9) => 0 compare(9, 10) => -1 compare(10, 1) => 1 compare(10, 2) => 1 compare(10, 3) => 1 compare(10, 4) => 1 compare(10, 5) => 1 compare(10, 6) => 1 compare(10, 7) => 1 compare(10, 8) => 1 compare(10, 9) => 1 compare(10, 10) => 0 $
0 コメント:
コメントを投稿