Skip to content

Instantly share code, notes, and snippets.

@myw
Created April 25, 2012 22:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myw/2493945 to your computer and use it in GitHub Desktop.
Save myw/2493945 to your computer and use it in GitHub Desktop.
Anonymous Y Combinator Factorial Example in Matlab
Y = @(f) ...
feval( ...
@(x) f(@(v) feval(feval(x, x), v)), ...
@(x) f(@(v) feval(feval(x, x), v)));
if_ = @(pred, iftrue, iffalse) ...
[ arrayfun(@(~) iftrue(), zeros( pred())) ...
arrayfun(@(~) iffalse(), zeros(~pred())) ];
then_ = @(x) x;
else_ = @(x) x;
fct = @(f) @(n) ...
if_(@() n > 0, ...
then_(@() n * f(n - 1)), ...
else_(@() 1));
feval(Y(fct), 99)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment