You can do this in Matlab using an anonymous function that uses the dbstack () introspection to get the function literal itself and then evaluate it. (I admit that this is a hoax because dbstack should be considered extralinguistic, but it is available in all Matlabs.)
f = @(x) ~x || feval(str2func(getfield(dbstack, 'name')), x-1)
This is an anonymous function that counts from x and then returns 1. This is not very useful, because Matlab does not have an operator ?: and forbids if-blocks inside anonymous functions, so it is difficult to construct a base register / recursive step form.
You can demonstrate that it is recursive by calling f (-1); it will count indefinitely and, ultimately, will produce a maximum recursion error.
>> f(-1) ??? Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available stack space can crash MATLAB and/or your computer.
And you can call an anonymous function directly, without binding it to any variable, passing it directly to feval.
>> feval(@(x) ~x || feval(str2func(getfield(dbstack, 'name')), x-1), -1) ??? Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available stack space can crash MATLAB and/or your computer. Error in ==> create@(x)~x||feval(str2func(getfield(dbstack,'name')),x-1)
To make something useful out of this, you can create a separate function that implements the recursive logic of the step, using "if" to protect the recursive case from being evaluated.
function out = basecase_or_feval(cond, baseval, fcn, args, accumfcn) %BASECASE_OR_FEVAL Return base case value, or evaluate next step if cond out = baseval; else out = feval(accumfcn, feval(fcn, args{:})); end
Given that there is a factorial.
recursive_factorial = @(x) basecase_or_feval(x < 2,... 1,... str2func(getfield(dbstack, 'name')),... {x-1},... @(z)x*z);
And you can call it without reference.
>> feval( @(x) basecase_or_feval(x < 2, 1, str2func(getfield(dbstack, 'name')), {x-1}, @(z)x*z), 5) ans = 120