Re: Gather, take, newbie questions

2013-12-11 Thread David Warring
Hi Jacinta,
I get a bit further with my build of Perl 6, which at least runs:

% perl6 fib.pl
0

Are you maybe using an old Perl6? Try:

% perl6 -e'say $*PERL'

(name = rakudo, compiler = {name = rakudo, ver =
2013.11-22-g262e600, release-number = , build-date =
2013-12-05T22:52:45Z, codename = }).hash


 ===SORRY!===
 Confused at line 19, near }\n\nsub fib


- David


Re: Gather, take, newbie questions

2013-12-11 Thread Larry Wall
On Thu, Dec 12, 2013 at 02:05:35PM +1100, Jacinta Richardson wrote:
:sub MAIN($terms = 35, $maximum = 4_000_000) {
: my @sequence = gather for fibonacci($maximum) - $number {
: state $count = 0;
: take $number if $number  $maximum  $count++  $terms;
: };
: 
: say @sequence;
:}
: 
:sub fibonacci($maximum) {
:my @numbers := 0, 1, *+* ... *  $maximum;
:return @numbers;
:}

Your main problem here is that your series is terminated backwards.
The ... runs until the right side matches, and and 0  $maximum,
so it terminates on 0.  So you want *  $maximum.  You can also use a ^
to exclude the last value whwere it becomes true, so you can just say

0, 1, *+* ...^ *  $maximum

and then you don't have to put in the extra check in your loop.

But you don't really need the loop either.  A more succinct way to
pick the first N terms is to use slice subscripting to pick out the 0th
to the $term'th - 1 element (again, using ^ to exclude the final value):

say (0, 1, *+* ...^ *  $maximum)[0 ..^ $terms];

We also often like to abbreviate 0 ..^ $terms down to ^$terms like this

say (0, 1, *+* ...^ *  $maximum)[^$terms];

But usually we just limit it by number of terms, and don't care about the
maximum:

say (0, 1, *+* ... *)[^$terms];

Hope this helps...

Larry