開発環境
- 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)の Incremental development の Exercise.を取り組んでみる。
Incremental development の Exercise.
コード(Emacs)
#!/usr/bin/env perl6 # -*- coding: utf-8 -*- sub hypotenuse0($x, $y) { return 0.0; } say hypotenuse1(3, 4); sub hypotenuse1($x, $y) { say '$x is ', $x; say '$y is ', $y; return 0.0; } say hypotenuse1(3, 4); sub hypotenuse2($x, $y) { my $xsquared = $x * $x; my $ysquared = $y * $y; say '$xsquared is ', $xsquared; say '$ysquared is ', $ysquared; return 0.0; } say hypotenuse2(3, 4); sub hypotenuse3($x, $y) { my $xsquared = $x * $x; my $ysquared = $y * $y; my $squared = $xsquared + $ysquared; say '$squared is ', $squared; return 0.0; } say hypotenuse3(3, 4); sub hypotenuse($x, $y) { my $xsquared = $x * $x; my $ysquared = $y * $y; my $squared = $xsquared + $ysquared; my $result = sqrt $squared; return $result; } say hypotenuse(3, 4); say hypotenuse(3, 4) == 5;
入出力結果(Terminal, REPL)
$ ./sample_hypotenuse.pl $x is 3 $y is 4 0 $x is 3 $y is 4 0 $xsquared is 9 $ysquared is 16 0 $squared is 25 0 5 True $
0 コメント:
コメントを投稿