tags:

views:

138

answers:

3

As we all know that we can send any number of arguments to a subroutine in Perl. Would the following example be a correct demonstration of recursion to display 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 it is a lame example… but my aim is to know whether this type of implementation would still qualify to be an example for recursion?
Hoping for no brickbats :)

Edit:

depending upon some condition, if the program decides to send only one argument or two or multiple arguments to itself… would that be a valid characteristic?

+2  A: 

Yes. Its a recursive function. It meets the required conditions of

  • There should be a way to terminate recursion. In your case when $_[0] becomes 5
  • Recursive call should move towards the terminating case. You pass $_[0] + 1 to recursive calls.
codaddict
so it doesn't matter whether we are sending 1, 2 or more arguments in each function call? (depending upon some condition)
topgun_ivard
Actually neither of these requirements are needed for a function to qualify as a recursive function. Only that the function calls itself. In languages that don't have a loop construct (lisp for example) you use recursion to loop. And a recursion that never terminates the same as `while(1)...`.
slebetman
@topgun_ivard: No, it doesn't. The number of arguments received by a function has absolutely nothing to do with whether it's recursive or not. "Recursive" just means that it calls itself.
Dave Sherohman
+4  A: 

A function is recursive if it calls itself. Your recursion function calls itself, so it's recursive. codaddict's conditions are necessary for a recursive function to work properly, but a function could still be recursive if they weren't met. (It would just be recursive and buggy.)

cjm
+2  A: 

Your program works and it is recursive, which is a great place to start. However, it's difficult to read and is not very flexible in its usage. Here's 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);
FM
I thought the whole point of Perl was to make the code hard to read...
Lasse V. Karlsen
@Lasse: if you think that you've got a lot to learn about Perl.
Ether
The whole point of Perl is to get work done.
brian d foy