daily pastebin goal
36%
SHARE
TWEET

[Perl 6] Fibonacci number in closures.

cwchen Nov 28th, 2017 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top