Perl | Tail Calls in Function Recursion
Last Updated :
21 Nov, 2019
Recursion in Perl is any function that does not uses the for
loop or the while
loop, instead calls itself during the execution of the program and the corresponding function is known as recursive function.
Tail recursive function is a special case of recursion in which the function call statement is performed as the final action of the procedure. So return loop(..);
would work, but return loop() + operation;
would not. Tail recursion (or tail-end recursion) is particularly useful, and often easy to handle in implementations. The implementation of tail-recursion is mainly done to avoid the space occupied by stack data structure which keeps track of the data returned from the previous recursion call statement.
Following are few examples for a better understanding of the concept:
Examples 1:
sub recurse_fact
{
my $x = $_ [0];
if ( $x == 0 || $x == 1)
{
return 1;
}
else
{
return $x * recurse_fact( $x - 1);
}
}
sub tail_recurse_fact
{
my ( $ans , $x ) = @_ ;
if ( $x == 0 || $x == 1)
{
return $ans ;
}
else
{
return tail_recurse_fact( $ans * $x , $x - 1);
}
}
$a = 5;
print "Factorial of a number $a " ,
"through recursion is " , recurse_fact( $a );
print "\nFactorial of a number $a " ,
"through tail-recursion is " ,
tail_recurse_fact(1, $a );
|
Output:
Factorial of a number 5 through recursion is 120
Factorial of a number 5 through tail-recursion is 120
In the non-tail version, the compiler needs to keep track of the number you’re going to multiply it with, whereas in the tail-call version the compiler can realize that the only work left to do is another function call and it can forget about all of the variables and states used in the current function.
Examples 2:
use strict;
use warnings;
sub recurse_fib
{
my $n = shift ;
if ( $n == 1 or $n == 2)
{
return 1
}
return (recurse_fib( $n - 1) +
recurse_fib( $n - 2));
}
sub tail_recurse_fib
{
my ( $n , $a , $b ) = @_ ;
if ( $n == 1)
{
return $a
}
if ( $n == 2)
{
return $b
}
else
{
return tail_recurse_fib( $n - 1, $b , $a + $b )
}
}
$a = 10;
print "Fibonacci upto $a through recursion is " ,
recurse_fib( $a );
print "\nFibonacci upto $a through tail-recursion is " ,
tail_recurse_fib( $a , 1, 1);
|
Output:
Fibonacci upto 10 through recursion is 55
Fibonacci upto 10 through tail-recursion is 55
Use of goto
statement to demonstrate Tail Recursion: goto
will transfer the compiler to the subroutine of the given name from the currently running subroutine. This will replace the function call and creates a recursion process in the same way.
use warnings;
sub recurse
{
my $i = shift ;
return if $i == 0;
recurse( $i - 1);
}
sub tail_recurse
{
my $i = shift ;
return if $i == 0;
@_ = ( $i - 1);
goto &tail_recurse;
}
print "recursing\n" ;
recurse(200);
print "tail_recursing\n" ;
tail_recurse(200);
|
Output:
recursing
tail_recursing
In the above example the recurse
function will produce a fatal error of ‘deep recursion’ while the tail_recurse
will work fine.
Elimination of Tail Call
Tail recursive is better than non-tail recursive as tail-recursive can be optimized by modern compilers. What a modern compiler do to optimize the tail recursive code is known as tail call elimination. Tail call elimination saves stack space. It replaces a function call with a goto statement. This really isn’t elimination – it is an optimization.
Sometimes a deeply recursive method is the easiest solution to a problem, but if you recurse more than a couple of hundred calls you’ll hit Perl’s “deep recursion” error. That is the reason why tail-recursive is used over non-tail recursive codes in Perl.
If we take a closer look at the above-discussed function, we can remove the last call with goto. Below are examples of tail call elimination.
Examples 1: Factorial of a number
sub tail_recurse_fact
{
my $ans = shift ;
my $x = shift ;
if ( $x == 0 || $x == 1)
{
return $ans ;
}
else
{
@_ = ( $x * $ans , $x - 1);
goto &tail_recurse_fact;
}
}
$a = 5;
print "Factorial of a number $a " ,
"through tail-recursion is " ,
tail_recurse_fact(1, $a );
|
Output:
Factorial of a number 5 through tail-recursion is 120
Examples 2: Fibonacci upto n
use strict;
use warnings;
sub tail_recurse_fib
{
my $n = shift ;
my $a = shift ;
my $b = shift ;
if ( $n == 1)
{
return $a
}
if ( $n == 2)
{
return $b
}
else
{
@_ = ( $n - 1, $b , $a + $b );
goto &tail_recurse_fib;
}
}
$a = 10;
print "Fibonacci upto $a through tail-recursion is " ,
tail_recurse_fib( $a , 1, 1);
|
Output:
Fibonacci upto 10 through recursion is 55
Fibonacci upto 10 through tail-recursion is 55
Share your thoughts in the comments
Please Login to comment...