Created
October 10, 2020 13:56
-
-
Save lechimp-p/e414b9ad694063cb67f2f935c4bdbd1b to your computer and use it in GitHub Desktop.
This is Aphyrs "Reversing the technical interview" in PHP.
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 | |
// This is Aphyrs "Reversing the technical interview" in PHP. | |
// https://aphyr.com/posts/340-reversing-the-technical-interview | |
function cons($a, $b) { | |
return function ($x) use ($a, $b) { | |
return $x ? $a : $b; | |
}; | |
} | |
function nth($n, $l) { | |
if ($n == 0) { | |
return $l(true); | |
} | |
if (is_null($l)) { | |
return null; | |
} | |
return nth($n-1, $l(false)); | |
} | |
function pprint($l) { | |
echo "["; | |
while($l) { | |
echo $l(true); | |
$l = $l(false); | |
if (!is_null($l)) { | |
echo ", "; | |
} | |
} | |
echo "]"; | |
} | |
function reverse($l, $r = null) { | |
if (is_null($l)) { | |
return $r; | |
} | |
return reverse($l(false), cons($l(true), $r)); | |
} | |
pprint(reverse(cons(1, cons(2, cons(3, null))))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment