How to resolve the algorithm Anonymous recursion step by step in the Raku programming language
How to resolve the algorithm Anonymous recursion step by step in the Raku programming language
Table of Contents
Problem Statement
While implementing a recursive function, it often happens that we must resort to a separate helper function to handle the actual recursion. This is usually the case when directly calling the current function would waste too many resources (stack space, execution time), causing unwanted side-effects, and/or the function doesn't have the right arguments and/or return values. So we end up inventing some silly name like foo2 or foo_helper. I have always found it painful to come up with a proper name, and see some disadvantages: Some languages allow you to embed recursion directly in-place. This might work via a label, a local gosub instruction, or some special keyword. Anonymous recursion can also be accomplished using the Y combinator.
If possible, demonstrate this by writing the recursive version of the fibonacci function (see Fibonacci sequence) which checks for a negative argument before doing the actual recursion.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Anonymous recursion step by step in the Raku programming language
Source code in the raku programming language
sub fib($n) {
die "Naughty fib" if $n < 0;
return {
$_ < 2
?? $_
!! &?BLOCK($_-1) + &?BLOCK($_-2);
}($n);
}
say fib(10);
constant @fib = 0, 1, *+* ... *;
say @fib[10];
You may also check:How to resolve the algorithm Parameterized SQL statement step by step in the Clojure programming language
You may also check:How to resolve the algorithm Letter frequency step by step in the Groovy programming language
You may also check:How to resolve the algorithm Loops/For step by step in the Clojure programming language
You may also check:How to resolve the algorithm 2048 step by step in the Perl programming language
You may also check:How to resolve the algorithm Modular exponentiation step by step in the Erlang programming language