示例#1
0
    return branch(null, thunk($n), thunk(false), null) !== $n;
}, 'until1' => function ($n) {
    $x = $n % 8;
    return until(function ($args) use($x) {
        list($m, $arr) = $args;
        return [$m === $x, [$m + 1, snoc($m, $arr)]];
    }, [0, []]) !== [$x + 1, upto($x + 1)];
}, 'trampoline1' => function ($n) {
    $x = $n % 8;
    return trampoline(y(function ($f, $m, $n, $_) {
        return $m < $n ? [false, $f($m + 1, $n)] : [true, $m];
    }, 0, $x)) !== $x;
}, 'loop1' => function ($x) {
    $n = $x % 8;
    $lhs = loop(function ($x, $m) use($n) {
        return [$m >= $n, snoc($m, $x)];
    }, []);
    $rhs = upto($n + 1);
    return $lhs === $rhs ? 0 : dump(get_defined_vars());
}, 'y1' => function ($x) {
    $n = $x % 4;
    return y(function ($f, $m) use($n) {
        return $m === $n ? $m : $f($m + 1);
    }, 0) !== $n;
}, 'stream_take1' => function ($x) {
    $n = $x % 8;
    $lhs = upto($n);
    $rhs = stream_take($n, y(function ($f, $n, $_) {
        return [$n, $f($n + 1)];
    }, 0));
    return $lhs === $rhs ? 0 : dump(get_defined_vars());
示例#2
0
});
defun('trampoline', function ($f) {
    for ($stop = false; !$stop; list($stop, $f) = $f(null)) {
    }
    return $f;
});
defun('y', function ($f) {
    $cf = curry($f);
    return curry(function ($x) use($cf) {
        return $cf(y($cf), $x);
    });
});
defun('stream_take', function ($n, $s) {
    return trampoline(y(function ($f, $x, $n, $s, $_) {
        if (!$n) {
            return [true, $x];
        }
        list($h, $t) = $s(null);
        return [false, $f(snoc($h, $x), $n - 1, $t)];
    }, [], $n, $s));
});
defun('stream_drop', function ($n, $s) {
    return trampoline(y(function ($f, $n, $s, $_) {
        if (!$n) {
            return [true, $s];
        }
        list($h, $t) = $s(null);
        return [false, $f($n - 1, $t)];
    }));
});
defun('dump', nary('var_dump', 1));