Recursive lambda functions?

TheFlyingFiddle kurtyan at student.chalmers.se
Mon Dec 30 04:26:32 PST 2013


On Monday, 30 December 2013 at 11:23:39 UTC, Ilya Yaroshenko 
wrote:
> Hello!
>
> Is there any ability to lambda function call itself?
>
> Is it correct feature for D?
>
> Best Regards,
> Ilya

Well it's possible to do this (sort of).

//You could for instance define the Factorial function like this.

int delegate(int) f;
f = (int x) => x > 0 ? f(x - 1) * x : 1;

It's not very pretty and offers little compared to just make it a 
normal function.
void f(int x) { return x > 0 ? f(x - 1) * x : 1; }


More information about the Digitalmars-d-learn mailing list