PHP code example of loophp / combinator
1. Go to this page and download the library: Download loophp/combinator library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
loophp / combinator example snippets
declare(strict_types=1);
rs;
// Lambda calculus: I combinator correspond to λa.a
Combinators::I()('a'); // a
// Lambda calculus: K combinator correspond to λa.λb.a (or λab.a)
Combinators::K()('a')('b'); // a
// Lambda calculus: C combinator correspond to λf(λa(λb(fba)))
// and thus: C K a b = b
Combinators::C()(Combinators::K())('a')('b'); // b
// Lambda calculus: Ki combinator correspond to λa.λb.b (or λab.b)
Combinators::Ki()('a')('b'); // b
declare(strict_types=1);
namespace Test;
// Example 1
$factorialGenerator = static fn (Closure $fact): Closure =>
static fn (int $n): int => (0 === $n) ? 1 : ($n * $fact($n - 1));
$factorial = Combinators::Y()($factorialGenerator);
var_dump($factorial(6)); // 720
// Example 2
$fibonacciGenerator = static fn (Closure $fibo): Closure =>
static fn (int $number): int => (1 >= $number) ? $number : $fibo($number - 1) + $fibo($number - 2);
$fibonacci = Combinators::Y()($fibonacciGenerator);
var_dump($fibonacci(10)); // 55