CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
27
Task/Anonymous-recursion/C++/anonymous-recursion-1.cpp
Normal file
27
Task/Anonymous-recursion/C++/anonymous-recursion-1.cpp
Normal 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);
|
||||
}
|
||||
}
|
||||
16
Task/Anonymous-recursion/C++/anonymous-recursion-2.cpp
Normal file
16
Task/Anonymous-recursion/C++/anonymous-recursion-2.cpp
Normal 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);
|
||||
}
|
||||
|
|
@ -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))))
|
||||
|
|
@ -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))))))
|
||||
|
|
@ -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))))))
|
||||
|
|
@ -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))))))
|
||||
9
Task/Anonymous-recursion/D/anonymous-recursion-1.d
Normal file
9
Task/Anonymous-recursion/D/anonymous-recursion-1.d
Normal 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));
|
||||
}
|
||||
18
Task/Anonymous-recursion/D/anonymous-recursion-2.d
Normal file
18
Task/Anonymous-recursion/D/anonymous-recursion-2.d
Normal 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));
|
||||
}
|
||||
18
Task/Anonymous-recursion/Deja-Vu/anonymous-recursion-1.djv
Normal file
18
Task/Anonymous-recursion/Deja-Vu/anonymous-recursion-1.djv
Normal 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
|
||||
15
Task/Anonymous-recursion/Deja-Vu/anonymous-recursion-2.djv
Normal file
15
Task/Anonymous-recursion/Deja-Vu/anonymous-recursion-2.djv
Normal 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
|
||||
2
Task/Anonymous-recursion/Ela/anonymous-recursion-1.ela
Normal file
2
Task/Anonymous-recursion/Ela/anonymous-recursion-1.ela
Normal 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
|
||||
1
Task/Anonymous-recursion/Ela/anonymous-recursion-2.ela
Normal file
1
Task/Anonymous-recursion/Ela/anonymous-recursion-2.ela
Normal file
|
|
@ -0,0 +1 @@
|
|||
fix f = f (& fix f)
|
||||
21
Task/Anonymous-recursion/Elena/anonymous-recursion.elena
Normal file
21
Task/Anonymous-recursion/Elena/anonymous-recursion.elena
Normal 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".
|
||||
].
|
||||
].
|
||||
Loading…
Add table
Add a link
Reference in a new issue