2017年4月26日水曜日

開発環境

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 の Exercises.を取り組んでみる。

Exercises.

コード(Emacs)

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

sub hypotenuse($x, $y) {
    return 0.0;
}

say hypotenuse(1, 1);
say hypotenuse(3, 4);
say hypotenuse(1, sqrt 3);

sub hypotenuse1($x, $y) {
    my $x-squared = $x ** 2;
    my $y-squared = $y ** 2;
    say '$x-squared is ', $x-squared;
    say '$y-squared is ', $y-squared;
    return 0.0;
}

say hypotenuse1(1, 1);
say hypotenuse1(3, 4);
say hypotenuse1(1, sqrt 3);

sub hypotenuse2($x, $y) {
    my $x-squared = $x ** 2;
    my $y-squared = $y ** 2;
    my $sum = $x-squared + $y-squared;
    say '$sum is ', $sum;    
    return 0.0;
}

say hypotenuse2(1, 1);
say hypotenuse2(3, 4);
say hypotenuse2(1, sqrt 3);

sub hypotenuse3($x, $y) {
    my $x-squared = $x ** 2;
    my $y-squared = $y ** 2;
    my $sum = $x-squared + $y-squared;
    my $result = sqrt $sum;
    return $result;
}

say hypotenuse3(1, 1);
say hypotenuse3(3, 4);
say hypotenuse3(1, sqrt 3);

入出力結果(Terminal, REPL)

$ ./sample_inc.pl
0
0
0
$x-squared is 1
$y-squared is 1
0
$x-squared is 9
$y-squared is 16
0
$x-squared is 1
$y-squared is 3
0
$sum is 2
0
$sum is 25
0
$sum is 4
0
1.4142135623731
5
2
$

0 コメント:

コメントを投稿