Calling anonymous delegate recursively ?

tsukikage tsukikage at gmail.com
Sat Jan 8 12:07:01 PST 2011


Thank Pelle , and others.

I'm thinking ways to do this task :
http://rosettacode.org/wiki/Anonymous_recursion

With this last version of Y-combinator
http://rosettacode.org/wiki/Y_combinator#D ,
it look like this:
ulong fib(long n) {
   if(n < 0) throw new Exception("No negative") ;
   return Y((ulong delegate(ulong) self) {
     return (ulong m) {
       return (m <= 1) ? m : self(m-1) + self(m-2) ;
     } ;
   })(n) ;
}
and works. Only that this Y-combinator seems induced a lot of 
overhead(four return statements in the Y's definition).

 From D document, if i not misunderstood, a delegate has 2 property .ptr 
(frame pointer) and .funcptr (address of function), so the delegate 
should be a structure? Would there be a hack to get access to this 
structure? It seems not now.

Thanks again.

Pelle wrote:
> On 01/08/2011 04:45 PM, tsukikage wrote:
>> eg. to return a fibonacci delegate:
>>
>> return (ulong m) {
>> if(m < 2) return m ;
>> return _self_ref(m-1)+_self_ref(m-2) ;
>> } ;
>>
>> Is it possible? Thank you!
> 
> http://en.wikipedia.org/wiki/Fixed_point_combinator#Y_combinator
> 
> I don't think there's a built in way to self-recurse.


More information about the Digitalmars-d-learn mailing list