The Little PHPer - 4. Numbers Games May 11, 2018 numbers: consider only nonnegative integers. tup: a list of numbers, could be empty. Scheme PHP [Source Code] add1 function add1 ($n) {return $n + 1; } sub1 function sub1 ($n) {return $n - 1; } zero? function is_zero ($n) {return 0 === $n; } + function plus ($n, $m) {return is_zero($m) ? $n : add1(plus($n, sub1($m))); } − function minus ($n, $m) {return is_zero($m) ? $n : sub1(minus($n, sub1($m))); } addtup function addtup ($tup) {return is_nulll($tup) ? 0 : plus(car($tup), addtup(cdr($tup))); } × function x ($n, $m) {return is_zero($m) ? 0 : plus($n, x($n, sub1($m))); } tup+ function tupplus ($tup1, $tup2) {return is_nulll($tup1) ? $tup2 : (is_nulll($tup2) ? $tup1 : cons(plus(car($tup1), car($tup2)), tupplus(cdr($tup1), cdr($tup2)))); } > function gt ($n, $m) {return is_zero($n) ? FALSE : is_zero($m) || gt(sub1($n), sub1($m)); } < function lt ($n, $m) {return is_zero($m) ? FALSE : is_zero($n) || lt(sub1($n), sub1($m)); } = function is_eqn ($n, $m) {return is_zero($n) ? is_zero($m) : (is_zero($m) ? FALSE : is_eqn(sub1($n), sub1($m))); } ↑ function power ($n, $m) {return is_zero($m) ? 1 : x($n, power($n, sub1($m))); } ÷ function division ($n, $m) {return is_zero($m) ? PHP_INT_MAX : (lt($n, $m) ? 0 : add1(division(minus($n, $m), $m))); } length function length ($l) {return is_nulll($l) ? 0 : add1(length(cdr($l))); } pick function pick ($n, $l) {return is_zero($n) ? NULL : (is_one($n) ? car($l) : pick(sub1($n), cdr($l))); } rempick function rempick ($n, $l) {return is_zero($n) ? $l : (is_nulll($l) ? [] : (is_one($n) ? cdr($l) : cons(car($l), rempick(sub1($n), cdr($l))))); } number? function is_number ($n) {return is_int($n) && $n >=0; } no-nums function no_nums ($l) {return is_nulll($l) ? [] : (is_number(car($l)) ? no_nums(cdr($l)) : cons(car($l), no_nums(cdr($l)))); } all-nums function all_nums ($l) {return is_nulll($l) ? [] : (is_number(car($l)) ? cons(car($l), all_nums(cdr($l))) : all_nums(cdr($l))); } eqan? function is_eqan ($a1, $a2) {return is_number($a1) && is_number($a2) ? is_eqn($a1, $a2) : (is_number($a1) || is_number($a1) ? FALSE : is_eq($a1, $a2)); } occur function occur ($a, $l) {return is_nulll($l) ? 0 : (is_eqan(car($l), $a) ? add1(occur($a, cdr($l))) : occur($a, cdr($l))); } one? function is_one ($n) {return is_eqn($n, 1); }