Perl6の内部表現 |
Takahiro Shimizu
|
my $str_value = 'hello world!';
$str_value.say; # hello world!
my $sample_value = 'hello world!';
$sample_value.say; # hello world!
$sample_value = '31';
$sample_value.say; # 31
say($sample_value * 3);
my Int $int_value = 31;
$int_value = "hello"; # Compile error!
my subset Fizz of Int where * %% 3;
my subset Buzz of Int where * %% 5;
my subset FizzBuzz of Int where Fizz&Buzz;
my subset Number of Int where none Fizz|Buzz;
proto sub fizzbuzz ($) { * }
multi sub fizzbuzz (FizzBuzz) { "FuzzBuzz" }
multi sub fizzbuzz (Fizz) { "Fizz" }
multi sub fizzbuzz (Buzz) { "Buzz" }
multi sub fizzbuzz (Number $number) { $number }
fizzbuzz($_).say for 1..15;
#! nqp
sub fib($n) {
$n < 2 ?? $n !! fib($n-1) + fib($n - 2);
}
my $N := 29;
my $t0 := nqp::time_n();
my $z := fib($N);
my $t1 := nqp::time_n();
nqp::say("fib($N) = " ~ fib($N));
nqp::say("time = " ~ ($t1-$t0));