Is my Fibonacci routine an example of recursion in Perl?

As we all know, we can send any number of arguments to a routine in Perl. Would the following example be the correct demonstration of recursion to display a Fibonacci series (5 values)?

#!/usr/bin/perl -w use strict; sub recursion { if ($_[0] && $_[2]) { print $_[2],"\n"; if ($_[0] < 5) { return recursion($_[0] + 1, $_[2], $_[1] + $_[2]); } else { return $_[0]; } } elsif ($_[0] && !$_[1] && !$_[2]) { print "0\n"; return recursion($_[0], 0, 1); } } print "\nFibo Series : \n"; recursion(1); print "\nEnter to exit"; <>; 

I know this is a lame example ... but my goal is to find out if this type of implementation can be an example for recursion?
Hope without bricks :)

Edit:

depending on some condition, if the program decides to send only one argument or two or more arguments to itself ... would this be a valid characteristic?

+4
source share
4 answers

Yes. Its recursive function. It satisfies the required conditions.

  • There must be a way to stop recursion. In your case, when $_[0] becomes 5
  • The recursive call should move in the direction of the ending case. You pass $_[0] + 1 for recursive calls.
+2
source

A function is recursive if it calls itself. Your recursion function calls itself, so it is recursive. For a recursive function to work properly, codaddict conditions are necessary, but a function can be recursive if they were not executed. (This will be just recursive and buggy.)

+4
source

Your program works, and it is recursive, and this is a great place to start. However, it is difficult to read and not very flexible to use. Here is an alternative with a few suggestions:

 use strict; use warnings; sub fib_up_to { # Unpack @_ for readability and maintainability. my ($max, $i, $j) = @_; # Handle the first call by the user, who normally would supply only the max. # Note that we test whether $i and $j are defined rather than just # evaluating their truth: 0 is defined but false in Perl. ($i, $j) = (0, 1) unless defined $i and defined $j; return unless defined $max and $max >= 0; # Check for terminal condition. return if $i > $max; # Do stuff and then recurse. print $i, "\n"; fib_up_to($max, $j, $i + $j); } # Give your function a meaningful name. Also, let it be run from the command # line, which is handy during development. For example: # # perl fib_up_to.pl 100 # perl fib_up_to.pl 100 8 13 fib_up_to(@ARGV); 
+2
source

Fibonacci numbers are a favorite way to demonstrate recursion (along with the factorial that I use in Mastering Perl ). Your example is fine, but you should also know that you do not need this feature in many cases.

Here's an iterative solution that creates a sequence from the lower end, not the high end. You do not need to return, because you already know the answers to the calculations necessary for the next step:

 use v5.20; use experimental qw(signatures); no warnings qw(experimental::signatures); sub fibonacci ( $n ) { my @numbers = qw( 0 1 ); foreach ( 2 .. $n ) { $numbers[ $_ ] = $numbers[ $_ - 1 ] + $numbers[ $_ - 2 ]; } return @numbers[ 0 .. $n ]; } my @series = fibonacci( 1 ); say "Fibo Series : @series"; 

This gets even better, because you can change this to use state to remember previous calculations. You should use an array reference, since state cannot initialize the array, but this is not very important:

 use v5.22; use experimental qw(signatures postderef); no warnings qw(experimental::signatures experimental::postderef); sub fibonacci ( $n ) { state $numbers = [ qw( 0 1 ) ]; foreach ( $#$numbers .. $n ) { $numbers->[ $_ ] = $numbers->[ $_ - 1 ] + $numbers->[ $_ - 2 ]; } return $numbers->@[ 0 .. $n ]; } my @series = fibonacci( 17 ); say "Fibo Series : @series"; 
0
source

All Articles