Created
March 25, 2020 08:26
-
-
Save talyian/2d3a04fdd3a9137b8f1debaca24f27ac to your computer and use it in GitHub Desktop.
This file contains 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 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