What's the shortest way to define an anonymous recursive function in Octave? I love functional programming in Octave, but it's rather unwieldy in practice. I'm wondering about the shortest way to define an anonymous recursive function.

I have some ideas, but I'm wondering if there is a way to combine these ideas to make them even shorter (or equally short but more versatile). For the sake of this question, let's recursively count down to zero (just to keep the payload as simple as possible).

If my reasoning is correct, none of the variable names I used in the following examples should overlap. The desired function is q(n), which should always return zero. i is used as a counter variable, f is the recursive function which I have called g in the local scope of f.

44 bytes, "inline definition of f"

q=@(n)(f=@(g,i){@()g(g,i-1),i}{~i+1}())(f,n)


44 bytes, "argument list definition of f"

q=@(n,f=@(g,i){@()g(g,i-1),i}{~i+1}())f(f,n)


44 bytes, "separate definition of f"

f=@(i,g){@()g(i-1,g),i}{~i+1}();q=@(n)f(n,f)


41 bytes, "desired function as a return value"

f=@(g)@(n){@()g(g)(n-1),n}{~n+1}();q=f(f)


The current 'winner' is inspired by this answer by flawr. However, given the large range of different ways to do this, perhaps someone can think of an even shorter combination of methods.

The goal is of course to get it below 39 bytes for a "full" function, Try it online!
Octave, 39 bytes



q=f(f=@(g)@(n){@()g(g)(n-1),n}{~n+1}())


Try it online!