Chris, All
Apparently you can do recursive blocks, according to Bill Bumgarner.
The "trick" seems to be to declare the block variable with the __block modifier and break up declaration and initialization, this seems to keep both compiler and analyzer happy.
int main (int argc, const char * argv[]) { __block int (^factorial2)(int); factorial2 = ^(int n) { if (n == 1) return 1; else return n * factorial2(n-1); }; int i = factorial2(10);
return 0; }
Why do this instead of a block calling a recursive function? Same reason why we use blocks -- it's more convenient when you have to capture other local variables from the scope, otherwise the (private) recursive function will have to declare them all as parameters, and (public) wrapper function and block therein will have to copy or forward all the necessary variables to the recursive function.
Essentially writing a recursive function involves naming the private code that is recursive (in order to recurse). With blocks this naming can be hidden as an implementation detail behind the public function wrapping it. On 27/01/2011, at 11:20 AM, Chris Suter wrote: Hi Glen,
On Thu, Jan 27, 2011 at 2:04 PM, Chris Suter <email@hidden> wrote: For example:
[snip]
Sorry, my example had an obvious bug in it; I forgot to copy the block before returning it, but hopefully you got my point. Also, sorry about the formatting. I blame Gmail.
Kind regards,
Chris
Cheers, Glen Low
--- pixelglow software | simply brilliant stuff aim: pixglen twitter: pixelglow
|