www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Calling anonymous delegate recursively ?

reply tsukikage <tsukikage gmail.com> writes:
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!
Jan 08 2011
parent reply Pelle <pelle.mansson gmail.com> writes:
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.
Jan 08 2011
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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.
Jan 08 2011
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
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.
Jan 08 2011
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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..
Jan 08 2011
prev sibling next sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
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.
Jan 08 2011
prev sibling parent tsukikage <tsukikage gmail.com> writes:
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.
Jan 08 2011