11

我对使用定点组合器的实际示例感兴趣(例如 C++ 中的y 组合器。您是否曾经在真实的实时代码中使用带有egg绑定的定点组合器?

我发现鸡蛋中的这个例子有点密集:

void egg_example()
{
    using bll::_1;
    using bll::_2;

    int r =
        fix2(
            bll::ret<int>(
                // \(f,a) -> a == 0 ? 1 : a * f(a-1)
                bll::if_then_else_return( _2 == 0,
                    1,
                    _2 * lazy(_1)(_2 - 1)
                )
            )
        ) (5);

    BOOST_CHECK(r == 5*4*3*2*1);
}

你能解释一下这一切是如何运作的吗?

是否有一个很好的简单示例,可能使用绑定可能比这个更少的依赖项?

4

3 回答 3

34

这里是将相同的代码转换为boost::bind通知 y-combinator 及其在 main 函数中的应用程序站点。我希望这有帮助。

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>

// Y-combinator compatible factorial
int fact(boost::function<int(int)> f,int v)
{
  if(v == 0)
    return 1;
  else
    return v * f(v -1);
}

// Y-combinator for the int type
boost::function<int(int)>
    y(boost::function<int(boost::function<int(int)>,int)> f)
{
  return boost::bind(f,boost::bind(&y,f),_1);
}


int main(int argc,char** argv)
{
  boost::function<int(int)> factorial = y(fact);
  std::cout << factorial(5) << std::endl;
  return 0;
}
于 2008-09-30T18:13:20.927 回答
9
#include <functional>
#include <iostream>

template <typename Lamba, typename Type>
auto y (std::function<Type(Lamba, Type)> f) -> std::function<Type(Type)>
{
    return std::bind(f, std::bind(&y<Lamba, Type>, f), std::placeholders::_1);
}

int main(int argc,char** argv)
{
    std::cout << y < std::function<int(int)>, int> ([](std::function<int(int)> f, int x) {
        return x == 0 ? 1 : x * f(x - 1);
    }) (5) << std::endl;
    return 0;
}
于 2012-08-15T11:48:53.000 回答
3

你能解释一下这一切是如何运作的吗?

fix2 是一个 y 组合子(具体来说,它是具有两个参数的函数的组合子;第一个参数是函数(用于递归),第二个参数是“适当的”函数参数)。它创建递归函数。

bll::ret(...) 似乎创建了某种形式的函数对象,其主体是

if(second arg == 0)
{
    return 1;
}
else
{
    return second arg * first arg(second arg - 1);
}

“惰性”可能是为了阻止第一个(函数)参数的无限扩展(阅读惰性和严格 y 组合器之间的区别以了解原因)。

代码非常可怕。匿名函数很不错,但是解决 C++ 缺乏语法支持的黑客行为使它们不值得付出努力。

于 2008-09-30T19:15:31.027 回答