2017年2月20日月曜日

開発環境

Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)のChapter 2(Variables, expressions and statements)のExercises 2-1、2.を取り組んでみる。

Exercises 2-1、2.

コード(Emacs)

#!/usr/bin/env perl6

# 1. volume of a sphere
my $pi = 3.14;
my $r = 5;
say 4 / 3 ** ($pi * $r ** 3);

# 2.
my $cover = 24.95;
my $discount = 0.4;
my $first_copy = 3;
my $additional_copy = 0.75;
my $copies = 60;

say $cover * 0.4 + $first_copy + ($copies - 1) * $additional_copy;

# 3.
my $easy = 8 * 60 + 15;
my $tempo = 7 * 60 + 12;
my $d = $easy + 3 * $tempo + $easy;
my $t = 6 * 60 * 60 + 52 * 60 + $d;
my $m = $t % (60 * 60);
my $h = $t - $m;

say $h / (60 * 60)  ~ ':' ~ ceiling($m / 60) ~ ' am'

入出力結果(Terminal)

$ perl6
You may want to `zef install Readline` or `zef install Linenoise` or use rlwrap for a line editor

To exit type 'exit' or '^D'
> $n = 42;
===SORRY!=== Error while compiling:
Variable '$n' is not declared
------> <BOL>⏏$n = 42;
> my $n = 42;
42
> 42 = $n;
Cannot modify an immutable Int
  in block <unit> at <unknown file> line 1

> my $x = $y = 1;
===SORRY!=== Error while compiling:
Variable '$y' is not declared
------> my $x = ⏏$y = 1;
> my $x, $y;
===SORRY!=== Error while compiling:
Variable '$y' is not declared
------> my $x, ⏏$y;
> my $x;
(Any)
> my $;
(Any)
> $x = $y = 1;
===SORRY!=== Error while compiling:
Variable '$y' is not declared
------> $x = ⏏$y = 1;
> my $y;
(Any)
> $x = $y = 1;
1
> my ($a, $b) = 1, 1;
(1 1)
> $a
1
> $b
1
> my ($c, $d);
((Any) (Any))
> $c = $d = 1;
1
> $a = 10.
===SORRY!===
Decimal point must be followed by digit
------> $a = 10.⏏<EOL>
Unsupported use of . to concatenate strings; in Perl 6 please use ~
at line 2
------> <BOL>⏏<EOL>

> xy;
===SORRY!=== Error while compiling:
Undeclared routine:
    xy used at line 1

> $x$y;
===SORRY!=== Error while compiling:
Two terms in a row
------> $x⏏$y;
    expecting any of:
        infix
        infix stopper
        statement end
        statement modifier
        statement modifier loop
> $x * $y;
1
$ ./sample2.pl
===SORRY!=== Error while compiling ./sample2.pl
Two terms in a row across lines (missing semicolon or comma?)
at ./sample2.pl:4
------> my $x = 10⏏<EOL>
    expecting any of:
        infix
        infix stopper
        postfix
        statement end
        statement modifier
        statement modifier loop
$ ./sample3.pl
2.14766981790803e-187
57.23
7:31 am
$

0 コメント:

コメントを投稿