digitalmars.D.learn - Calling anonymous delegate recursively ?
- tsukikage (6/6) Jan 08 2011 eg. to return a fibonacci delegate:
- Pelle (3/9) Jan 08 2011 http://en.wikipedia.org/wiki/Fixed_point_combinator#Y_combinator
- Andrej Mitrovic (18/18) Jan 08 2011 As a workaround you can do this for now:
- Stewart Gordon (5/7) Jan 08 2011 I'm not sure what D would gain in practice. If you want a function that...
- Andrej Mitrovic (2/9) Jan 08 2011 I don't know. Perhaps it would be useful in generics..
-
Stewart Gordon
(5/6)
Jan 08 2011
- tsukikage (22/35) Jan 08 2011 Thank Pelle , and others.
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
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
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
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
On 1/8/11, Stewart Gordon <smjg_1998 yahoo.com> wrote:On 08/01/2011 17:40, Andrej Mitrovic wrote: <snip>I don't know. Perhaps it would be useful in generics..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
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
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