Skip to content

Instantly share code, notes, and snippets.

@lechimp-p
Created October 10, 2020 13:56
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 lechimp-p/e414b9ad694063cb67f2f935c4bdbd1b to your computer and use it in GitHub Desktop.
Save lechimp-p/e414b9ad694063cb67f2f935c4bdbd1b to your computer and use it in GitHub Desktop.
This is Aphyrs "Reversing the technical interview" in PHP.
<?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