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,13 @@
let rec a k x1 x2 x3 x4 x5 =
if k <= 0 then
x4 () + x5 ()
else
let m = ref k in
let rec b () =
decr m;
a !m b x1 x2 x3 x4
in
b ()
let () =
Printf.printf "%d\n" (a 10 (fun () -> 1) (fun () -> -1) (fun () -> -1) (fun () -> 1) (fun () -> 0))

View file

@ -0,0 +1,25 @@
#import <Foundation/Foundation.h>
typedef NSInteger (^IntegerBlock)();
NSInteger A (NSInteger kParam, IntegerBlock x1, IntegerBlock x2, IntegerBlock x3, IntegerBlock x4, IntegerBlock x5) {
__block NSInteger k = kParam;
__block IntegerBlock B; // due to a GCC bug, we have to initialize on a separate line
B = ^ {
return A(--k, B, x1, x2, x3, x4);
};
return k <= 0 ? x4() + x5() : B();
}
IntegerBlock K (NSInteger n) {
IntegerBlock result = ^{return n;};
return [[result copy] autorelease];
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSInteger result = A(10, K(1), K(-1), K(-1), K(1), K(0));
NSLog(@"%d\n", result);
[pool drain];
return 0;
}

View file

@ -0,0 +1,73 @@
@protocol IntegerFun <NSObject>
-(NSInteger)call;
@end
NSInteger A (NSInteger kParam, id<IntegerFun> x1, id<IntegerFun> x2, id<IntegerFun> x3, id<IntegerFun> x4, id<IntegerFun> x5);
@interface B_Class : NSObject <IntegerFun> {
NSInteger *k;
id<IntegerFun> x1, x2, x3, x4;
}
-(id)initWithK:(NSInteger *)k x1:(id<IntegerFun>)x1 x2:(id<IntegerFun>)x2 x3:(id<IntegerFun>)x3 x4:(id<IntegerFun>)x4;
@end
@implementation B_Class
-(id)initWithK:(NSInteger *)_k x1:(id<IntegerFun>)_x1 x2:(id<IntegerFun>)_x2 x3:(id<IntegerFun>)_x3 x4:(id<IntegerFun>)_x4 {
if ((self = [super init])) {
k = _k;
x1 = [_x1 retain];
x2 = [_x2 retain];
x3 = [_x3 retain];
x4 = [_x4 retain];
}
return self;
}
-(void)dealloc {
[x1 release];
[x2 release];
[x3 release];
[x4 release];
[super dealloc];
}
-(NSInteger)call {
return A(--*k, self, x1, x2, x3, x4);
}
@end
NSInteger A (NSInteger k, id<IntegerFun> x1, id<IntegerFun> x2, id<IntegerFun> x3, id<IntegerFun> x4, id<IntegerFun> x5) {
id<IntegerFun> B = [[[B_Class alloc] initWithK:&k x1:x1 x2:x2 x3:x3 x4:x4] autorelease];
return k <= 0 ? [x4 call] + [x5 call] : [B call];
}
@interface K : NSObject <IntegerFun> {
NSInteger n;
}
-(id)initWithN:(NSInteger)n;
@end
@implementation K
-(id)initWithN:(NSInteger)_n {
if ((self = [super init])) {
n = _n;
}
return self;
}
-(NSInteger)call {
return n;
}
@end
int main(int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSInteger result = A(10,
[[[K alloc] initWithN:1] autorelease],
[[[K alloc] initWithN:-1] autorelease],
[[[K alloc] initWithN:-1] autorelease],
[[[K alloc] initWithN:1] autorelease],
[[[K alloc] initWithN:0] autorelease]);
NSLog(@"%ld\n", result);
[pool release];
return 0;
}

View file

@ -0,0 +1,21 @@
declare
fun {A K X1 X2 X3 X4 X5}
ReturnA = {NewCell undefined}
fun {B}
ReturnB = {NewCell undefined}
in
K := @K - 1
ReturnA := {A {NewCell @K} B X1 X2 X3 X4}
ReturnB := @ReturnA
@ReturnB
end
in
if @K =< 0 then ReturnA := {X4} + {X5} else _ = {B} end
@ReturnA
end
fun {C V}
fun {$} V end
end
in
{Show {A {NewCell 10} {C 1} {C ~1} {C ~1} {C 1} {C 0}}}

View file

@ -0,0 +1,7 @@
sub A($k is copy, &x1, &x2, &x3, &x4, &x5) {
$k <= 0
?? x4() + x5()
!! (my &B = { A(--$k, &B, &x1, &x2, &x3, &x4) })();
};
say A(10, {1}, {-1}, {-1}, {1}, {0});

View file

@ -0,0 +1,15 @@
fun a (k, x1, x2, x3, x4, x5) =
if k <= 0 then
x4 () + x5 ()
else let
val m = ref k
fun b () = (
m := !m - 1;
a (!m, b, x1, x2, x3, x4)
)
in
b ()
end
val () =
print (Int.toString (a (10, fn () => 1, fn () => ~1, fn () => ~1, fn () => 1, fn () => 0)) ^ "\n")

View file

@ -0,0 +1,22 @@
implement main
open core
clauses
run():-
console::init(),
stdio::write(a(10, {() = 1}, {() = -1}, {() = -1}, {() = 1}, {() = 0})).
class predicates
a : (integer K, function{integer} X1, function{integer} X2, function{integer} X3, function{integer} X4, function{integer} X5) -> integer Result.
clauses
a(K, X1, X2, X3, X4, X5) = R :-
KM = varM::new(K),
BM = varM{function{integer}}::new({() = 0}),
BM:value :=
{ () = BR :-
KM:value := KM:value-1,
BR = a(KM:value, BM:value, X1, X2, X3, X4)
},
R = if KM:value <= 0 then X4() + X5() else BM:value() end if.
end implement main

View file

@ -0,0 +1,29 @@
self.a = method(k, x1, x2, x3, x4, x5){
b = method(){
code.k = code.k - 1
return( self.a(code.k, code, code.x1, code.x2, code.x3, code.x4) )
}
b.k = k
b.x1 = x1
b.x2 = x2
b.x3 = x3
b.x4 = x4
b.x5 = x5
if(k <= 0){
return(self.apply(x4) + self.apply(x5))
}
else{
return(self.apply(b))
}
}
self.K = method(n){
f = method(){
return(code.n)
}
f.n = n
return(f)
}
self.a(10, self.K(1), self.K(-1), self.K(-1), self.K(1), self.K(0)).print()