Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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