This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,27 @@
double fib(const double n)
{
if(n < 0)
{
throw "Invalid argument passed to fib";
}
else
{
class actual_fib
{
public:
static double calc(const double n)
{
if(n < 2)
{
return n;
}
else
{
return calc(n-1) + calc(n-2);
}
}
};
return actual_fib::calc(n);
}
}

View file

@ -0,0 +1,16 @@
#include <functional>
using namespace std;
double fib(double n)
{
if(n < 0)
throw "Invalid argument";
function<double(double)> actual_fib = [&](double n)
{
if(n < 2) return n;
return actual_fib(n-1) + actual_fib(n-2);
};
return actual_fib(n);
}

View file

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

View file

@ -0,0 +1,8 @@
(defun fib (number)
"Fibonacci sequence function."
(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))))))

View file

@ -0,0 +1,5 @@
(defmacro recursive ((&rest parm-init-pairs) &body body)
(let ((hidden-name (gensym "RECURSIVE-")))
`(macrolet ((recurse (&rest args) `(,',hidden-name ,@args)))
(labels ((,hidden-name (,@(mapcar #'first parm-init-pairs)) ,@body))
(,hidden-name ,@(mapcar #'second parm-init-pairs))))))

View file

@ -0,0 +1,64 @@
(setf (symbol-function '!) (symbol-function 'funcall)
(symbol-function '!!) (symbol-function 'apply))
(defmacro ? (args &body body)
`(lambda ,args ,@body))
(defstruct combinator
(name nil :type symbol)
(function nil :type function))
(defmethod print-object ((combinator combinator) stream)
(print-unreadable-object (combinator stream :type t)
(format stream "~A" (combinator-name combinator))))
(defconstant +y-combinator+
(make-combinator
:name 'y-combinator
:function (? (f) (! (? (g) (! g g))
(? (g) (! f (? (&rest a)
(!! (! g g) a))))))))
(defconstant +z-combinator+
(make-combinator
:name 'z-combinator
:function (? (f) (! (? (g) (! f (? (x) (! (! g g) x))))
(? (g) (! f (? (x) (! (! g g) x))))))))
(defparameter *default-combinator* +y-combinator+)
(defmacro with-y-combinator (&body body)
`(let ((*default-combinator* +y-combinator+))
,@body))
(defmacro with-z-combinator (&body body)
`(let ((*default-combinator* +z-combinator+))
,@body))
(defun x-call (x-function &rest args)
(apply (funcall (combinator-function *default-combinator*) x-function) args))
(defmacro x-function ((name &rest args) &body body)
`(lambda (,name)
(lambda ,args
(macrolet ((,name (&rest args)
`(funcall ,',name ,@args)))
,@body))))
(defmacro x-defun (name args &body body)
`(defun ,name ,args
(x-call (x-function (,name ,@args) ,@body) ,@args)))
;;;; examples
(x-defun factorial (n)
(if (zerop n)
1
(* n (factorial (1- n)))))
(x-defun fib (n)
(case n
(0 0)
(1 1)
(otherwise (+ (fib (- n 1))
(fib (- n 2))))))

View file

@ -0,0 +1,9 @@
uint fib(in uint n) pure nothrow {
immutable self = &__traits(parent, {});
return (n < 2) ? n : self(n - 1) + self(n - 2);
}
void main() {
import std.stdio;
writeln(fib(39));
}

View file

@ -0,0 +1,18 @@
import std.stdio;
int fib(in int n) pure nothrow {
assert(n >= 0);
return (new class {
static int opCall(in int m) pure nothrow {
if (m < 2)
return m;
else
return opCall(m - 1) + opCall(m - 2);
}
})(n);
}
void main() {
writeln(fib(39));
}

View file

@ -0,0 +1,18 @@
Y f:
labda y:
labda:
f y @y
call dup
labda fib n:
if <= n 1:
1
else:
fib - n 1
fib - n 2
+
Y
set :fibo
for j range 0 10:
print fibo j

View file

@ -0,0 +1,15 @@
fibo-2 n:
n 0 1
labda times back-2 back-1:
if = times 0:
back-2
elseif = times 1:
back-1
elseif = times 2:
+ back-1 back-2
else:
recurse -- times back-1 + back-1 back-2
call
for j range 0 10:
print fibo-2 j

View file

@ -0,0 +1,2 @@
fib n | n < 0 = fail "Negative n"
| else = fix (\f n -> if n < 2 then n else f (n - 1) + f (n - 2)) n

View file

@ -0,0 +1 @@
fix f = f (& fix f)

View file

@ -0,0 +1,21 @@
#define std'dictionary'*.
#define std'patterns'*.
#symbol fibo : i =
[
Control ifNot:(i < 0).
#if (i < 2)
? [ ^ i. ]
| [ ^ fibo::(i - 2) + fibo::(i - 1). ].
].
#symbol Program =
[
loop &&from:-1 &to:10 run: i =
[
'program'output << "%nfib(" << i << ")=".
'program'output << fibo::i | << "failed".
].
].