function treeMap($list, $func, $acc) { $iter = function ($list, $acc) use(&$iter, $func) { if ($list === null) { return $acc; } $element = car($list); if (isPair($element)) { $newAcc = treeMap($element, $func, $acc); } else { $newAcc = $func($element, $acc); } return $iter(cdr($list), $newAcc); }; return $iter($list, $acc); }
public function testNilIsNotAPair() { $list = nil(); $this->assertFalse(isPair($list)); }
/** * Returns the first element of a list for whose car a procedure returns true * or false if no match is found. * * @param callable $proc a function that takes one argument and returns a * boolean * @param Cons $alist an association list of key-value pairs */ function assp($proc, $alist) { if (isPair($alist)) { $car = car($alist); try { $x = car($car); } catch (\InvalidArgumentException $e) { throw new \InvalidArgumentException("improperly formed alist {$alist}"); } if ($proc($x)) { return $car; } else { return assp($proc, cdr($alist)); } } else { return false; } }
function walkStar($v, $s) { $v = walk($v, $s); if (isVariable($v)) { return $v; } elseif (isPair($v)) { return cons(walkStar(car($v), $s), walkStar(cdr($v), $s)); } else { return $v; } }