user
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:

:   :   * 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

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.

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.

Related tasks
:   * Y combinator
assistant
(formerly Perl 6)

**Works with**: Rakudo version 2015.12

In addition to the methods in the Perl entry above, and the Y-combinator described in Y\_combinator, you may also refer to an anonymous block or function from the inside:

```
sub fib($n) {
    die "Naughty fib" if $n < 0;
    return {
        $_ < 2
            ?? $_
            !!  &?BLOCK($_-1) + &?BLOCK($_-2);
    }($n);
}

say fib(10);
```

However, using any of these methods is insane, when Raku provides a sort of inside-out combinator that lets you define lazy infinite constants, where the demand for a particular value is divorced from dependencies on more primitive values. This operator, known as the sequence operator, does in a sense provide anonymous recursion to a closure that refers to more primitive values.

```
constant @fib = 0, 1, *+* ... *;
say @fib[10];
```

Here the closure, \*+\*, is just a quick way to write a lambda, -> $a, $b { $a + $b }. The sequence operator implicitly maps the two arguments to the -2nd and -1st elements of the sequence. So the sequence operator certainly applies an anonymous lambda, but whether it's recursion or not depends on whether you view a sequence as iteration or as simply a convenient way of memoizing a recursion. Either view is justifiable.

At this point someone may complain that the solution is doesn't fit the specified task because the sequence operator doesn't do the check for negative. True, but the sequence operator is not the whole of the solution; this check is supplied by the subscripting operator itself when you ask for @fib[-1]. Instead of scattering all kinds of arbitrary boundary conditions throughout your functions, the sequence operator maps them quite naturally to the boundary of definedness at the start of a list.