Advertisement
cwchen

[Perl 6] Fibonacci number in closures.

Nov 28th, 2017
3,564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 0.34 KB | None | 0 0
  1. sub fib {
  2.     my $a = 0;
  3.     my $b = 1;
  4.    
  5.     sub {
  6.         my $out = $a;
  7.         my $c = $a + $b;
  8.         $a = $b;
  9.         $b = $c;
  10.        
  11.         $out;
  12.     }
  13. }
  14.  
  15. my $f = fib();
  16.  
  17. my @arr = (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89);
  18.  
  19. loop (my $i = 0; $i < @arr.elems; $i++) {
  20.     $f() == @arr[$i] or die "Wrong value";
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement