View mode: basic / threaded / horizontal-split · Log in · Help
January 08, 2011
Calling anonymous delegate recursively ?
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!
January 08, 2011
Re: Calling anonymous delegate recursively ?
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.
January 08, 2011
Re: Calling anonymous delegate recursively ?
As a workaround you can do this for now:

import std.stdio;

enum deleg = returnFib();
ulong delegate(ulong m) returnFib()
{
   return (ulong m)
   {
       if(m < 2)
           return m;
       return deleg(m-1)+deleg(m-2);
   };
}

void main()
{
   writeln(returnFib()(10));
}

Otherwise I'd really like the ability for a lambda to call itself.
Perhaps a feature request is in order.
January 08, 2011
Re: Calling anonymous delegate recursively ?
On 08/01/2011 16:00, Pelle wrote:
<snip>
> http://en.wikipedia.org/wiki/Fixed_point_combinator#Y_combinator
<snip>

How are you getting around D not supporting recursively defined types?

Stewart.
January 08, 2011
Re: Calling anonymous delegate recursively ?
On 08/01/2011 17:40, Andrej Mitrovic wrote:
<snip>
> Otherwise I'd really like the ability for a lambda to call itself.
> Perhaps a feature request is in order.

I'm not sure what D would gain in practice.  If you want a function that 
calls itself, why not just name the function?

Stewart.
January 08, 2011
Re: Calling anonymous delegate recursively ?
On 1/8/11, Stewart Gordon <smjg_1998@yahoo.com> wrote:
> On 08/01/2011 17:40, Andrej Mitrovic wrote:
> <snip>
>> Otherwise I'd really like the ability for a lambda to call itself.
>> Perhaps a feature request is in order.
>
> I'm not sure what D would gain in practice.  If you want a function that
> calls itself, why not just name the function?
>
> Stewart.
>

I don't know. Perhaps it would be useful in generics..
January 08, 2011
Re: Calling anonymous delegate recursively ?
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.
Top | Discussion index | About this forum | D home