Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/Anonymous-recursion/Objective-C/anonymous-recursion-1.m
Normal file
32
Task/Anonymous-recursion/Objective-C/anonymous-recursion-1.m
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface AnonymousRecursion : NSObject { }
|
||||
- (NSNumber *)fibonacci:(NSNumber *)n;
|
||||
@end
|
||||
|
||||
@implementation AnonymousRecursion
|
||||
- (NSNumber *)fibonacci:(NSNumber *)n {
|
||||
int i = [n intValue];
|
||||
if (i < 0)
|
||||
@throw [NSException exceptionWithName:NSInvalidArgumentException
|
||||
reason:@"fibonacci: no negative numbers"
|
||||
userInfo:nil];
|
||||
int result;
|
||||
if (i < 2)
|
||||
result = 1;
|
||||
else
|
||||
result = [[self performSelector:_cmd withObject:@(i-1)] intValue]
|
||||
+ [[self performSelector:_cmd withObject:@(i-2)] intValue];
|
||||
return @(result);
|
||||
}
|
||||
@end
|
||||
|
||||
int main (int argc, const char *argv[]) {
|
||||
@autoreleasepool {
|
||||
|
||||
AnonymousRecursion *dummy = [[AnonymousRecursion alloc] init];
|
||||
NSLog(@"%@", [dummy fibonacci:@8]);
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
26
Task/Anonymous-recursion/Objective-C/anonymous-recursion-2.m
Normal file
26
Task/Anonymous-recursion/Objective-C/anonymous-recursion-2.m
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
int fib(int n) {
|
||||
if (n < 0)
|
||||
@throw [NSException exceptionWithName:NSInvalidArgumentException
|
||||
reason:@"fib: no negative numbers"
|
||||
userInfo:nil];
|
||||
int (^f)(int);
|
||||
__block __weak int (^weak_f)(int); // block cannot capture strong reference to itself
|
||||
weak_f = f = ^(int n) {
|
||||
if (n < 2)
|
||||
return 1;
|
||||
else
|
||||
return weak_f(n-1) + weak_f(n-2);
|
||||
};
|
||||
return f(n);
|
||||
}
|
||||
|
||||
int main (int argc, const char *argv[]) {
|
||||
@autoreleasepool {
|
||||
|
||||
NSLog(@"%d", fib(8));
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
25
Task/Anonymous-recursion/Objective-C/anonymous-recursion-3.m
Normal file
25
Task/Anonymous-recursion/Objective-C/anonymous-recursion-3.m
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#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[]) {
|
||||
@autoreleasepool {
|
||||
|
||||
NSLog(@"%d", fib(8));
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue