Skip to content

Instantly share code, notes, and snippets.

@danierdev
Last active April 7, 2016 14:17
Show Gist options
  • Save danierdev/055ab517b6e38d3998c02f1308675d9c to your computer and use it in GitHub Desktop.
Save danierdev/055ab517b6e38d3998c02f1308675d9c to your computer and use it in GitHub Desktop.
Y Combinator in PHP7
<?php
function Y($le) {
return (function ($f) {
return $f($f);
})(function ($f) use ($le) {
return $le(function ($x) use ($f) {
return $f($f)($x);
});
});
}
$factorial = Y(function ($fac) {
return function ($n) use ($fac) {
return $n <= 2 ? $n : $n * $fac($n - 1);
};
});
echo $factorial(5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment