langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,33 @@
#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:[NSNumber numberWithInt:i-1]] intValue]
+ [[self performSelector:_cmd withObject:[NSNumber numberWithInt:i-2]] intValue];
return [NSNumber numberWithInt:result];
}
@end
int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
AnonymousRecursion *dummy = [[AnonymousRecursion alloc] init];
NSLog(@"%@", [dummy fibonacci:[NSNumber numberWithInt:8]]);
[dummy release];
[pool release];
return 0;
}

View 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[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"%d", fib(8));
[pool release];
return 0;
}