Last active
April 7, 2016 14:17
-
-
Save danierdev/055ab517b6e38d3998c02f1308675d9c to your computer and use it in GitHub Desktop.
Y Combinator in PHP7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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