List Generator

sub fib_generator { state @fib_generator; my ($n) = @_; if ($n < @fib_generator) { return $fib_generator[$n]; } if (not @fib_generator) { @fib_generator = (0, 1); } while (@fib_generator <= $n) { push @fib_generator, $fib_generator[-2] + $fib_generator[-1]; } return $fib_generator[$n]; }