title: Recursive Anonymous Function Matlab
body: <p>I know that this is not what anonymous functions are made for, but just as a puzzle I tried to make a recursive function via anonymous functions. The prototype of recursive functions obviously is the factorial function. The problem is that it is difficult to make a case distinction <em>within</em> the anonymous functions. What I managed to do so far is following:</p>
  
  <pre><code>f=@(cn,n,f)eval('if n&gt;1; f(cn*n,n-1,f);else;ans=cn;end');
  f=@(n)f(1,n,f);
  </code></pre>
  
  <p>Or alternatively:</p>
  
  <pre><code>f=@(cn,n,f)eval('if n&gt;1; f(cn*n,n-1,f);else;disp(cn);end');
  f=@(n)f(1,n,f);
  </code></pre>
  
  <p>What is not very satisfactory is that you still cannot use this function when directly assigning, <code>a=f(3)</code> still produces an error, since <code>eval</code> does not get a value.</p>
  
  <p>So my question is, can you actually do a recursive function via anonymous functions that e.g. calculates factorial in a way that allows e.g. <code>a=f(3)</code> with relying only on native matlab functions (or functions you can create in the command line, as I did in my example)?</p>
  
  <p>PS: I know this does not have any practical use, it is just a challenge on how much you can bend and abuse Matlab's syntax.</p>