Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -2,8 +2,8 @@
"Fibonacci sequence function."
(if (< number 0)
(error "Error. The number entered: ~A is negative" number)
(labels ((fib1 (n a b)
(labels ((fib (n a b)
(if (= n 0)
a
(fib1 (- n 1) b (+ a b)))))
(fib1 number 0 1))))
(fib (- n 1) b (+ a b)))))
(fib number 0 1))))

View file

@ -1,14 +1,14 @@
import std.stdio, std.exception;
int fib(in uint arg) pure nothrow @safe @nogc {
assert(arg >= 0);
int fib(int arg) pure {
enforce(arg >= 0);
return function int (int n) pure nothrow {
auto self = __traits(parent, {});
return function uint(in uint n) pure nothrow @safe @nogc {
static immutable self = &__traits(parent, {});
return (n < 2) ? n : self(n - 1) + self(n - 2);
}(arg);
}
void main() {
import std.stdio;
39.fib.writeln;
}

View file

@ -10,11 +10,11 @@
#symbol program =
[
control from:-1 &to:10 &do: i
control forrange &int:-1 &int:10 &do: (&int:i)
[
console << "fib(" << i << ")=".
console writeLine:(fibo:i) | onInvalidArgumentError: e
console writeLine:(fibo:i) | if &InvalidArgumentError: e
[
console writeLine:"invalid".
].

View file

@ -0,0 +1,4 @@
Fib(n)={
my(F=(k,f)->if(k<2,k,f(k-1,f)+f(k-2,f)));
if(n<0,(-1)^(n+1),1)*F(abs(n),F)
};

View file

@ -0,0 +1,4 @@
Fib(n)={
my(F=k->my(f=self());if(k<2,k,f(k-1)+f(k-2)));
if(n<0,(-1)^(n+1),1)*F(abs(n))
};

View file

@ -2,12 +2,15 @@
function fib($n) {
if ($n < 0)
throw new Exception('Negative numbers not allowed');
else if ($n < 2)
return 1;
else {
$f = __FUNCTION__;
return $f($n-1) + $f($n-2);
}
$f = function($n) { // This function must be called using call_user_func() only
if ($n < 2)
return 1;
else {
$g = debug_backtrace()[1]['args'][0];
return call_user_func($g, $n-1) + call_user_func($g, $n-2);
}
};
return call_user_func($f, $n);
}
echo fib(8), "\n";
?>

View file

@ -0,0 +1,32 @@
$ include "seed7_05.s7i";
const func integer: fib (in integer: x) is func
result
var integer: fib is 0;
local
const func integer: fib1 (in integer: n) is func
result
var integer: fib1 is 0;
begin
if n < 2 then
fib1 := n;
else
fib1 := fib1(n-2) + fib1(n-1);
end if;
end func;
begin
if x < 0 then
raise RANGE_ERROR;
else
fib := fib1(x);
end if;
end func;
const proc: main is func
local
var integer: i is 0;
begin
for i range 0 to 4 do
writeln(fib(i));
end for;
end func;

View file

@ -0,0 +1,9 @@
fun fix f x = f (fix f) x
fun fib n =
if n < 0 then raise Fail "Negative"
else
fix (fn fib =>
(fn 0 => 0
| 1 => 1
| n => fib (n-1) + fib (n-2))) n

View file

@ -0,0 +1,11 @@
fun fib n =
let
fun fib 0 = 0
| fib 1 = 1
| fib n = fib (n-1) + fib (n-2)
in
if n < 0 then
raise Fail "Negative"
else
fib n
end

View file

@ -0,0 +1,6 @@
fun fib 0 = 0
| fib 1 = 1
| fib n = fib (n-1) + fib (n-2)
val fib = fn n => if n < 0 then raise Fail "Negative"
else fib n

View file

@ -0,0 +1,17 @@
@(do
(defmacro recursive ((. parm-init-pairs) . body)
(let ((hidden-name (gensym "RECURSIVE-")))
^(macrolet ((recurse (. args) ^(,',hidden-name ,*args)))
(labels ((,hidden-name (,*[mapcar first parm-init-pairs]) ,*body))
(,hidden-name ,*[mapcar second parm-init-pairs])))))
(defun fib (number)
(if (< number 0)
(error "Error. The number entered: ~a is negative" number)
(recursive ((n number) (a 0) (b 1))
(if (= n 0)
a
(recurse (- n 1) b (+ a b))))))
(put-line `fib(10) = @(fib 10)`)
(put-line `fib(-1) = @(fib -1)`))