RosettaCodeData/Task/Anonymous-recursion/00DESCRIPTION

19 lines
1.2 KiB
Text
Raw Permalink Normal View History

2016-12-05 22:15:40 +01:00
While implementing a recursive function, it often happens that we must resort to a separate   ''helper function''   to handle the actual recursion.
2013-04-10 14:58:50 -07:00
2016-12-05 22:15:40 +01:00
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.
2013-04-10 14:58:50 -07:00
2016-12-05 22:15:40 +01:00
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:
2013-04-10 14:58:50 -07:00
2016-12-05 22:15:40 +01:00
::*   You have to think up a name, which then pollutes the namespace
::*   Function is created which is called from nowhere else
::*   The program flow in the source code is interrupted
2013-04-10 14:58:50 -07:00
2016-12-05 22:15:40 +01:00
Some languages allow you to embed recursion directly in-place.   This might work via a label, a local ''gosub'' instruction, or some special keyword.
2013-04-10 14:58:50 -07:00
2016-12-05 22:15:40 +01:00
Anonymous recursion can also be accomplished using the   [[Y combinator]].
2013-04-10 14:58:50 -07:00
2016-12-05 22:15:40 +01:00
;Task:
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.
<br><br>