Skip to content

Instantly share code, notes, and snippets.

@i-am-tom
Last active October 1, 2016 22:34
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 i-am-tom/692c0f44ef2880973fdcb1875fd80248 to your computer and use it in GitHub Desktop.
Save i-am-tom/692c0f44ef2880973fdcb1875fd80248 to your computer and use it in GitHub Desktop.
The Y (and U) combinator(s), with an example, implemented in PHP.
<?php
$fib = function ($f) {
return function ($n) use ($f) {
switch ($n) {
case 0: return 0;
case 1: return 1;
default:
return $f ($n - 1)
+ $f ($n - 2);
}
};
};
Y ($fib) (10);
<?php
function U ($f) {
return $f ($f);
}
function Y ($f) {
return U (function ($x) use ($f) {
return $f (function ($y) use ($x) {
return U ($x) ($y);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment