Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplication with Perl 6 Sequence Whatever (...) operator

Tags:

perl

raku

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?

like image 389
Yet Another Geek Avatar asked Oct 21 '11 17:10

Yet Another Geek


2 Answers

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).

like image 78
hobbs Avatar answered Sep 27 '22 18:09

hobbs


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

my $n = 6;
my @powers_of_six := { 1, $n, { $^a * $n } ... *);
like image 32
mob Avatar answered Sep 27 '22 17:09

mob