For instance, given a numerical pattern, Perl 6 can intuit the next numbers in the series. The statement “(1,3,5,7….*)” will return a set of twenty odd numbers, starting at one. The statement “(1,2,4….*)” will return a sequence of successive numbers raised by the power of two, each a double of the number preceding it.

In an example given by Larry Wall at YAPC:: EU 2010, we can define the parameters of a sequence, as in the example (the Fibonacci sequence), written in Perl 6:

(0,1, *+* ... *)[^20].perl.say;

output => (0,1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181)

Where  (0,1, *+* … *) is the sequence, ‘0,1’ and each subsequent number is the sum of the previous two. And [^20] is the number of resultant elements.

Geek curiosity: In this example, the maximum number of results is [^1476] that is 1.3069892237634e+308 (geek), if you exceed that number will appear INF in the final of the valid sequence.

Good Perl!

Advertisements