Advertisement
cwchen

[Perl 6] Fibonacci numbers in iterators.

Nov 28th, 2017
3,528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 0.26 KB | None | 0 0
  1. sub fib {
  2.     my $a = 0;
  3.     my $b = 1;
  4.    
  5.     gather {
  6.         loop {
  7.             take $a;
  8.             my $c = $a + $b;
  9.             $a = $b;
  10.             $b = $c;
  11.         }
  12.     }
  13. }
  14.  
  15. # (0 1 1 2 3 5 8 13 21 34 55)
  16. fib[10] == 55 or die "Wrong value";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement