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?
See an extensive explanation here.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With