Recursive lambda functions?
Meta
jared771 at gmail.com
Mon Dec 30 14:20:02 PST 2013
On Monday, 30 December 2013 at 21:15:43 UTC, Ilya Yaroshenko
wrote:
> On Monday, 30 December 2013 at 12:24:28 UTC, lomereiter wrote:
>> Use Y combinator?
>> http://forum.dlang.org/thread/mailman.157.1385247528.2552.digitalmars-d-learn@puremagic.com#post-l6rgfq:24g5o:241:40digitalmars.com
>
> This is not lambda =(
>
> I want something like
>
> enum factrorial5 = (a => a == 0 ? 1 : a * __lambda(a-1))(5);
> //Recursive pure lambda function
You can do this with __traits(parent, {}), but it's ugly.
auto fact = function(int n)
{
if (n == 0)
{
return 1;
}
else
{
enum self = __traits(parent, {});
return n * self(n - 1);
}
};
Unfortunately, a shorter version written with the lambda syntax
doesn't work, it just segfaults:
auto fact = (int n) =>
(n < 2)
? 1
: n * __traits(parent, {})(n - 1);
More information about the Digitalmars-d-learn
mailing list