mckayb/phantasy-recursion-schemes

PHP中实现的常见递归方案

v1.0.0 2019-02-24 21:29 UTC

This package is auto-updated.

Last update: 2024-09-25 09:50:30 UTC


README

PHP中实现的常见递归方案。

入门指南

安装

composer require mckayb/phantasy-recursion-schemes

使用方法

// This comes from https://github.com/mckayb/phantasy-types
use function Phantasy\Types\sum;
use function Phantasy\Recursion\cata;

$LL = sum('LinkedList', [
	'Cons' => ['head', 'tail'],
	'Nil' => []
]);
$LL->map = function (callable $f) {
	return $this->cata([
		'Cons' => function ($head, $tail) use ($f) {
			return $this->Cons($head, $f($tail));
 		},
		'Nil' => function () {
			return $this->Nil();
		}
	]);
};
$LL->isNil = function () {
	return $this->cata([
		'Cons' => function ($head, $tail) {
			return false;
		},
		'Nil' => function () {
			return true;
		}
	]);
};
$alg = function ($x) {
	return $x->isNil() ? 0 : $x->head + $x->tail;
};
$a = $LL->Cons(3, $LL->Cons(2, $LL->Cons(1, $LL->Nil())));
echo cata($alg, $a);
// 6

欲了解更多信息,请参阅文档!

包含内容

  • 猫形态,鞍形态,双曲形态

贡献

发现错误?想要添加任何功能?只需创建一个问题或发起一个拉取请求。

需要更多吗?

对于此存储库中未包含的其他助手,请查看