Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Y-combinator in D?

I'm trying to learn the Y-combinator better (I sort of understand it in Scheme) and implement it in D 2.0, and I'm failing pretty miserably:

auto fact = delegate(uint delegate(uint) recurse)
{
    return delegate(uint n)
    {
        return n > 1 ? n * recurse(n - 1) : 1;
    };
};

fact(fact)(5);

This doesn't work, for the obvious reason that I can't pass fact to fact (what would its type be?). And besides, I still need fact's name to pass to itself, so it wouldn't work anyway, right?

But... how do I go about implementing the Y-combinator in D?

like image 328
user541686 Avatar asked Aug 04 '11 07:08

user541686


2 Answers

See an extensive explanation here.

like image 200
Eli Barzilay Avatar answered Sep 30 '22 03:09

Eli Barzilay


It's a know limitation of D (and C/C++) that functions that deal with typed self references are impossible (last time I checked) to declare.

I find this ironic because it amounts to a limitation of syntax (the length of the function prototype is infinite) or naming convention (the same problem but with name mangling) rather than anything semantic or architectural.

like image 23
BCS Avatar answered Sep 30 '22 04:09

BCS