This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -1,4 +1,4 @@
double fib(const double n)
double fib(double n)
{
if(n < 0)
{
@ -6,10 +6,9 @@ double fib(const double n)
}
else
{
class actual_fib
struct actual_fib
{
public:
static double calc(const double n)
static double calc(double n)
{
if(n < 2)
{

View file

@ -0,0 +1,26 @@
double fib(double n)
{
if(n < 0)
{
throw "Invalid argument passed to fib";
}
else
{
struct actual_fib
{
double operator()(double n)
{
if(n < 2)
{
return n;
}
else
{
return (*this)(n-1) + (*this)(n-2);
}
}
};
return actual_fib()(n);
}
}

View file

@ -0,0 +1,15 @@
-module( anonymous_recursion ).
-export( [fib/1, fib_internal/1] ).
fib( N ) when N >= 0 ->
fib( N, 1, 0 ).
fib_internal( N ) when N >= 0 ->
Fun = fun (_F, 0, _Next, Acc ) -> Acc;
(F, N, Next, Acc) -> F( F, N - 1, Acc+Next, Next )
end,
Fun( Fun, N, 1, 0 ).
fib( 0, _Next, Acc ) -> Acc;
fib( N, Next, Acc ) -> fib( N - 1, Acc+Next, Next ).

View file

@ -0,0 +1,22 @@
#APPTYPE CONSOLE
FUNCTION Fibonacci(n)
IF n < 0 THEN
RETURN "Nuts!"
ELSE
RETURN Fib(n)
END IF
FUNCTION Fib(m)
IF m < 2 THEN
Fib = m
ELSE
Fib = Fib(m - 1) + Fib(m - 2)
END IF
END FUNCTION
END FUNCTION
PRINT Fibonacci(-1.5)
PRINT Fibonacci(1.5)
PRINT Fibonacci(13.666)
PAUSE

View file

@ -6,18 +6,18 @@ interface SelfApplicable<OUTPUT> {
}
class Utils {
public static <INPUT, OUTPUT> SelfApplicable<Function<Function<Function<INPUT, OUTPUT>, Function<INPUT, OUTPUT>>, Function<INPUT, OUTPUT>>> y(Class<INPUT> input, Class<OUTPUT> output) {
public static <INPUT, OUTPUT> SelfApplicable<Function<Function<Function<INPUT, OUTPUT>, Function<INPUT, OUTPUT>>, Function<INPUT, OUTPUT>>> y() {
return y -> f -> x -> f.apply(y.apply(y).apply(f)).apply(x);
}
public static <INPUT, OUTPUT> Function<Function<Function<INPUT, OUTPUT>, Function<INPUT, OUTPUT>>, Function<INPUT, OUTPUT>> fix(Class<INPUT> input, Class<OUTPUT> output) {
return y(input, output).apply(y(input, output));
public static <INPUT, OUTPUT> Function<Function<Function<INPUT, OUTPUT>, Function<INPUT, OUTPUT>>, Function<INPUT, OUTPUT>> fix() {
return Utils.<INPUT, OUTPUT>y().apply(Utils.<INPUT, OUTPUT>y());
}
public static long fib(int m) {
if (m < 0)
throw new IllegalArgumentException("n can not be a negative number");
return fix(Integer.class, Long.class).apply(
return Utils.<Integer, Long>fix().apply(
f -> n -> (n < 2) ? n : (f.apply(n - 1) + f.apply(n - 2))
).apply(m);
}

View file

@ -0,0 +1,18 @@
<?php
class fib_helper {
function __invoke($n) {
if ($n < 2)
return 1;
else
return $this($n-1) + $this($n-2);
}
}
function fib($n) {
if ($n < 0)
throw new Exception('Negative numbers not allowed');
$f = new fib_helper();
return $f($n);
}
echo fib(8), "\n";
?>

View file

@ -1,4 +1,5 @@
>>> Y = lambda f: (lambda x: x(x))(lambda y: lambda *args: f(y(y), *args))
>>>from functools import partial
>>> Y = lambda f: (lambda x: x(x))(lambda y: partial(f, lambda *args: y(y)(*args)))
>>> fib = lambda f, n: None if n < 0 else (0 if n == 0 else (1 if n == 1 else f(n-1) + f(n-2)))
>>> [ Y(fib)(i) for i in range(-2, 10) ]
[None, None, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

View file

@ -0,0 +1,14 @@
#lang racket
;; We use Z combinator (applicative order fixed-point operator)
(define Z
(λ (f)
((λ (x) (f (λ (g) ((x x) g))))
(λ (x) (f (λ (g) ((x x) g)))))))
(define fibonacci
(Z (λ (fibo)
(λ (n)
(if (<= n 2)
1
(+ (fibo (- n 1))
(fibo (- n 2))))))))