Question

I have seen examples of the Perl 6 whatever (...) operator in sequences, and I have tried to find out how to do a sequence which involves multiplications.

The operator does the following, if one starts with some numbers, one can specify a sequence of the numbers following it.

@natural = 1,2 ... *;
@powersOfTwo = 1,2,4 ... *;

and so on. One could also define a sequence using the previous numbers in the sequence as in the fibonacci numbers (shown in this question), where one does the following:

@fibonacci = 1,1, *+* ... *;

The problem is that the multiplication operator is * and the previous numbers are also represented with *.

While I can define a sequence using +, - and /, I can not seem to find a way of defining a sequence using *.

I have tried the following:

@powers = 1,2, *** ... *;

but it obviously does not work.

Does anyone know how to this?

Was it helpful?

Solution

For one thing, Perl 6 is sensitive to whitespace.

1, 2, * * * ... *

is perfectly legitimate and generates a sequence that's sort of like a multiplicative fibonacci; it's just a little bit hard to read. *** and * * * mean something different.

If the ambiguity bothers you, you can use an explicit block instead of the implicit one that using "whatever star" gives you:

1, 2, -> $a, $b { $a * $b } ... *

and

1, 2, { $^a * $^b } ... *

both produce the same sequence as 1, 2, * * * ... * does (tested in Rakudo).

OTHER TIPS

my @powers_of_two := { 1, 2, { $^a * 2 } ... *);

my $n = 6;
my @powers_of_six := { 1, $n, { $^a * $n } ... *);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top