Anonymous recursion: Difference between revisions

Line 735:
NSLog(@"%@", [dummy fibonacci:[NSNumber numberWithInt:8]]);
[dummy release];
 
[pool release];
return 0;
}</lang>
 
;With internal named recursive block:
{{works with|Mac OS X|10.6+}}
<lang php>#import <Foundation/Foundation.h>
 
int fib(int n) {
if (n < 0)
@throw [NSException exceptionWithName:NSInvalidArgumentException
reason:@"fib: no negative numbers"
userInfo:nil];
__block int (^f)(int);
f = ^(int n) {
if (n < 2)
return 1;
else
return f(n-1) + f(n-2);
};
return f(n);
}
 
int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"%d", fib(8));
 
[pool release];
Anonymous user