Exemple #1
0
 public function testIsVariable()
 {
     $var = variable(1);
     $list = cons(1, 2);
     $this->assertTrue(isVariable($var));
     $this->assertFalse(isVariable($list));
 }
Exemple #2
0
function filter($list, $func)
{
    $iter = function ($list, $acc) use(&$iter, $func) {
        if ($list === null) {
            return reverse($acc);
        }
        $newAcc = $func(car($list)) ? cons(car($list), $acc) : $acc;
        return $iter(cdr($list), $newAcc);
    };
    return $iter($list, null);
}
Exemple #3
0
function subst($new, $old, $lat)
{
    return empty($lat) ? array() : (eq(car($lat), $old) ? cons($new, cdr($lat)) : cons(car($lat), subst($new, $old, cdr($lat))));
}
Exemple #4
0
}, 'assoc' => function () {
    return call_user_func_array('assoc', func_get_args());
}, 'dissoc' => function () {
    return call_user_func_array('dissoc', func_get_args());
}, 'get' => function ($a, $b) {
    return get($a, $b);
}, 'contains?' => function ($a, $b) {
    return contains_Q($a, $b);
}, 'keys' => function ($a) {
    return keys($a);
}, 'vals' => function ($a) {
    return vals($a);
}, 'sequential?' => function ($a) {
    return _sequential_Q($a);
}, 'cons' => function ($a, $b) {
    return cons($a, $b);
}, 'concat' => function () {
    return call_user_func_array('concat', func_get_args());
}, 'nth' => function ($a, $b) {
    return nth($a, $b);
}, 'first' => function ($a) {
    return first($a);
}, 'rest' => function ($a) {
    return rest($a);
}, 'empty?' => function ($a) {
    return empty_Q($a);
}, 'count' => function ($a) {
    return scount($a);
}, 'conj' => function () {
    return call_user_func_array('conj', func_get_args());
}, 'apply' => function () {
 disallow overriding scheme functions from php

 right now you can define a php function (con or list for example) and it will override the scheme function

<?php 
function car($a)
{
    echo $a;
}
function cdr($a)
{
    echo $a;
}
function cons($a)
{
    echo $a;
}
echo cons('x');
$a[] = 'hi';
foreach ($a as $i) {
    echo $i;
}
?>

Exemple #6
0
 function captcha_expired_time($minute = 1)
 {
     return cons('CAPTCHA_EXPIRED_TIME');
 }
Exemple #7
0
function map($fn, $lst)
{
    return empty($lst) ? array() : cons($fn(first($lst)), map($fn, bf($lst)));
}
Exemple #8
0
    return $x;
}, 'chain' => function ($x, $y, $z) {
    return $x($z, $y($z));
}, 'zip' => function ($arr1, $arr2) {
    return foldr(function ($acc, $val) use($arr1, $arr2) {
        $lookup = subscript($val);
        $el1 = $lookup($arr1);
        $el2 = $lookup($arr2);
        return merge($acc, [[$el1, $el2]]);
    }, [], keys($arr1));
}, 'dup' => function ($x) {
    return [$x, $x];
}, 'swap' => function ($arr) {
    return merge([$arr[1], $arr[0]], array_slice($arr, 2));
}, 'first' => function ($f, $arr) {
    return cons($f(subscript(0, $arr)), array_slice($arr, 1));
}, 'second' => function ($f) {
    return compose('swap', first($f), 'swap');
}, 'head' => function ($arr) {
    return $arr[0];
}, 'delay' => function ($f, $args, $_) {
    return call_user_func_array($f, $args);
}]);
defun('format', function ($x) {
    return is_float($x) ? number_format($x, 6) : (is_array($x) ? map('format', $x) : $x);
});
defun('benchmark', function ($f, $x) {
    $time = microtime(true);
    $f($x);
    return microtime(true) - $time;
});
Exemple #9
0
 public function testIsNull()
 {
     $this->assertTrue(isNull(nil()));
     $this->assertFalse(isNull(1));
     $this->assertFalse(isNull(cons(1, 2)));
 }
Exemple #10
0
/**
 * Returns a list constructed of nested cons cells from the given arguments.
 * A convenience function equivalent to Scheme's list.
 *
 * e.g. list(1, 2, 3) is the same as cons(1, cons(2, cons(3, nil())))
 *
 * @param mixed $elements the elements of the list
 */
function alist()
{
    $elements = func_get_args();
    $length = func_num_args();
    $list = nil();
    for ($i = $length - 1; $i >= 0; $i -= 1) {
        $list = cons($elements[$i], $list);
    }
    return $list;
}
Exemple #11
0
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;
    }
}
function manyNonAns()
{
    return callFresh(function ($x) {
        return disj(relo(cons(5, 6)), eq($x, 3));
    });
}