March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -0,0 +1,23 @@
Fib(n) {
nold1 := 1
nold2 := 0
If n < 0
{
MsgBox, Positive argument required!
Return
}
Else If n = 0
Return nold2
Else If n = 1
Return nold1
Fib_Label:
t := nold2+nold1
If n > 2
{
n--
nold2:=nold1
nold1:=t
GoSub Fib_Label
}
Return t
}

View file

@ -1,10 +1,7 @@
)abbrev package TESTP TestPackage
Z ==> Integer
TestPackage : with
fib : Z -> Z
== add
fib x ==
x <= 0 => error "argument outside of range"
f : Reference((Z,Z,Z) -> Z) := ref((n, v1, v2) +-> 0)
f() := (n, v1, v2) +-> if n<2 then v2 else f()(n-1,v2,v1+v2)
f()(x,1,1)
#include "axiom"
Z ==> Integer;
fib(x:Z):Z == {
x <= 0 => error "argument outside of range";
f(n:Z,v1:Z,v2:Z):Z == if n<2 then v2 else f(n-1,v2,v1+v2);
f(x,1,1);
}

View file

@ -1,10 +1,10 @@
#include "aldor";
#include "aldorio";
import from Integer;
Z ==> Integer;
fib(x:Z):Z == {
x <= 0 => throw SyntaxException;
f(n:Z,v1:Z,v2:Z):Z == if n<2 then v2 else f(n-1,v2,v1+v2);
f(x,1,1);
}
stdout << fib(10000);
)abbrev package TESTP TestPackage
Z ==> Integer
TestPackage : with
fib : Z -> Z
== add
fib x ==
x <= 0 => error "argument outside of range"
f : Reference((Z,Z,Z) -> Z) := ref((n, v1, v2) +-> 0)
f() := (n, v1, v2) +-> if n<2 then v2 else f()(n-1,v2,v1+v2)
f()(x,1,1)

View file

@ -15,19 +15,18 @@
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];
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[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@autoreleasepool {
AnonymousRecursion *dummy = [[AnonymousRecursion alloc] init];
NSLog(@"%@", [dummy fibonacci:[NSNumber numberWithInt:8]]);
[dummy release];
AnonymousRecursion *dummy = [[AnonymousRecursion alloc] init];
NSLog(@"%@", [dummy fibonacci:@8]);
[pool release];
}
return 0;
}

View file

@ -5,21 +5,22 @@ int fib(int n) {
@throw [NSException exceptionWithName:NSInvalidArgumentException
reason:@"fib: no negative numbers"
userInfo:nil];
__block int (^f)(int);
f = ^(int n) {
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 f(n-1) + f(n-2);
return weak_f(n-1) + weak_f(n-2);
};
return f(n);
}
int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@autoreleasepool {
NSLog(@"%d", fib(8));
NSLog(@"%d", fib(8));
[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[]) {
@autoreleasepool {
NSLog(@"%d", fib(8));
}
return 0;
}

View file

@ -0,0 +1,9 @@
>>> from inspect import currentframe
>>> from types import FunctionType
>>> def myself (*args, **kw):
... caller_frame = currentframe(1)
... code = caller_frame.f_code
... return FunctionType(code, caller_frame.f_globals)(*args, **kw)
...
>>> print "factorial(5) =",
>>> print (lambda n:1 if n<=1 else n*myself(n-1)) ( 5 )