Skip to content

Instantly share code, notes, and snippets.

@wouterj
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save wouterj/82e57f015a69bb34e216 to your computer and use it in GitHub Desktop.

Select an option

Save wouterj/82e57f015a69bb34e216 to your computer and use it in GitHub Desktop.
<?hh
/*-------------------------*\
PROGRAMMING
WITH
NOTHING
\*-------------------------*/
/*
* A heavily based on http://codon.com/programming-with-nothing
*/
/*-------------------------*\
$PROGRAM
\*-------------------------*/
$zero = $f ==> $x ==> $x;
$one = $f ==> $x ==> $f($x);
$two = $f ==> $x ==> $f($f($x));
$three = $f ==> $x ==> $f($f($f($x)));
$five = $f ==> $x ==> $f($f($f($f($f($x)))));
$true = $x ==> $y ==> $x;
$false = $x ==> $y ==> $y;
$if = $f ==> $f;
$isZero = $f ==> ($f($x ==> $false))($true);
$increment = $n ==> $f ==> $x ==> $f(($n($f))($x));
$decrement = $n ==> $f ==> $x ==> (($n($g ==> $h ==> $h($g($f))))($y ==> $x))($y ==> $y);
$add = $m ==> $n ==> ($n($increment))($m);
$subtract = $m ==> $n ==> ($n($decrement))($m);
$multiply = $m ==> $n ==> ($n($add($m)))($zero);
$power = $m ==> $n ==> ($n($multiply($m)))($one);
$cons = $x ==> $y ==> $f ==> ($f($x))($y);
$car = $p ==> $p($x ==> $y ==> $x);
$cdr = $p ==> $p($x ==> $y ==> $y);
/*-------------------------*\
$HELPERS (not used in program)
\*-------------------------*/
$toNum = $f ==> ($f($x ==> $x + 1))(0);
$toBool = $f ==> (($if($f))(true))(false);
$toTextBool = $f ==> $toBool($f) ? 'true' : 'false';
// test script in a tweet by @mathiasverraes: https://gist.github.com/mathiasverraes/9046427
function it($m,$p){echo ($p?"✔":"✘")." It $m\n"; if(!$p)register_shutdown_function(function(){die(1);});}
/*-------------------------*\
$TESTS
\*-------------------------*/
it('has zero', $toNum($zero) === 0);
it('has one', $toNum($one) === 1);
it('has two', $toNum($two) === 2);
it('has three', $toNum($three) === 3);
it('has five', $toNum($five) === 5);
it('has true', $toBool($true) === true);
it('has false', $toBool($false) === false);
it('can find zero values', $toBool($isZero($zero)) === true);
it('can find non zero values', $toBool($isZero($three)) === false);
it('can increment values', $toNum($increment($one)) === 2);
it('can decrement values', $toNum($decrement($three)) === 2);
it('can add numbers', $toNum(($add($three))($one)) === 4);
it('can subtract numbers', $toNum(($subtract($five))($two)) === 3);
it('can multiply numbers', $toNum(($multiply($five))($three)) === 15);
it('can calculate the power of numbers', $toNum(($power($three))($three)) === 27);
$pair = ($cons($one))($two);
it('can find the car of a pair', $toNum($car($pair)) === 1);
it('can find the cdr of a pair', $toNum($cdr($pair)) === 2);
$list = ($cons($one))(($cons($two))($three));
it('can find the car of a big list', $toNum($car($list)) === 1);
it('can find the cdar of a big list', $toNum($car($cdr($list))) === 2);
it('can find the cddr of a big list', $toNum($cdr($cdr($list))) === 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment