Skip to content

Instantly share code, notes, and snippets.

@talyian
Created March 25, 2020 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talyian/2d3a04fdd3a9137b8f1debaca24f27ac to your computer and use it in GitHub Desktop.
Save talyian/2d3a04fdd3a9137b8f1debaca24f27ac to your computer and use it in GitHub Desktop.
<?php
function factorial($n) {
if (!$n)
return 1;
return $n * factorial($n-1);
}
function Y($f) {
$g = function ($x) use($f) {
return $f(function() use ($x) {
return call_user_func_array($x($x), func_get_args());
});
};
return $g($g);
}
$factorial = Y(function($f) {
return function($i) use($f) {
return $i < 1 ? 1 : ($i * $f($i-1));
};
});
echo $factorial(8);
echo "\n";
echo factorial(8);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment