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,30 @@
using System;
using System.Console;
module Fib
{
Fib(n : long) : long
{
def fib(m : long)
{
|0 => 1
|1 => 1
|_ => fib(m - 1) + fib(m - 2)
}
match(n)
{
|n when (n < 0) => throw ArgumentException("Fib() not defined on negative numbers")
|_ => fib(n)
}
}
Main() : void
{
foreach (i in [-2 .. 10])
{
try {WriteLine("{0}", Fib(i));}
catch {|e is ArgumentException => WriteLine(e.Message)}
}
}
}

View file

@ -0,0 +1,10 @@
let fib n =
let rec real = function
0 -> 1
| 1 -> 1
| n -> real (n-1) + real (n-2)
in
if n < 0 then
None
else
Some (real n)

View file

@ -0,0 +1,7 @@
let rec fix f x = f (fix f) x
let fib n =
if n < 0 then
None
else
Some (fix (fun f -> fun n -> if n <= 1 then 1 else f (n-1) + f (n-2)) n)

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;
}

View file

@ -0,0 +1,9 @@
function fiboRatio() as double
function fibo( double i, j ) as double
if j > 2e12 then return j / i
return fibo j, i + j
end function
return fibo 1, 1
end function
print fiboRatio

View file

@ -0,0 +1,10 @@
sub fib($n) {
die "Naughty fib" if $n < 0;
return {
$_ < 2
?? $_
!! &?BLOCK($_-1) + &?BLOCK($_-2);
}($n);
}
say fib(10);

View file

@ -0,0 +1,2 @@
constant @fib = 0, 1, *+* ... *;
say @fib[10];

View file

@ -0,0 +1,23 @@
% primitive recursion
/pfact {
{1} {*} primrec}.
%linear recursion
/lfact {
{dup 0 eq}
{pop 1}
{dup pred}
{*}
linrec}.
% general recursion
/gfact {
{0 eq}
{succ}
{dup pred}
{i *}
genrec}.
% binary recursion
/fib {
{2 lt} {} {pred dup pred} {+} binrec}.

View file

@ -0,0 +1,7 @@
(define fib
N -> (let A (/. A N
(if (< N 2)
N
(+ (A A (- N 2))
(A A (- N 1)))))
(A A N)))

View file

@ -0,0 +1,17 @@
fib() {
if test 0 -gt "$1"; then
echo "fib: fib of negative" 1>&2
return 1
else
(
fib2() {
if test 2 -gt "$1"; then
echo "$1"
else
echo $(( $(fib2 $(($1 - 1)) ) + $(fib2 $(($1 - 2)) ) ))
fi
}
fib2 "$1"
)
fi
}

View file

@ -0,0 +1,18 @@
$ for i in -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12; do
> fib $i
> done
fib: fib of negative
fib: fib of negative
0
1
1
2
3
5
8
13
21
34
55
89
144

View file

@ -0,0 +1,13 @@
#import nat
fib =
~&izZB?( # test the sign bit of the argument
<'fib of negative'>!%, # throw an exception if it's negative
{0,1}^?<a( # test the argument to a recursively defined function
~&a, # if the argument was a member of {0,1}, return it
sum^|W( # otherwise return the sum of two recursive calls
~&, # to the function thus defined
predecessor^~( # with the respective predecessors of
~&, # the given argument
predecessor)))) # and the predecessor thereof

View file

@ -0,0 +1,17 @@
include c:\cxpl\codes;
func Fib(X);
int X;
func ActualFib(N);
int N;
[if N<2 then return N
else return ActualFib(N-1) + ActualFib(N-2);
]; \ActualFib;
[if X<0 then [Text(0, "Error "); return 0]
else return ActualFib(X);
]; \Fib;
[IntOut(0, Fib(8)); CrLf(0);
IntOut(0, Fib(-2)); CrLf(0);
]