September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,8 +0,0 @@
|
|||
fib(n)
|
||||
{ if(n < 0)
|
||||
return error
|
||||
else if(n < 2)
|
||||
return 1
|
||||
else
|
||||
return n * fib(n-1)
|
||||
}
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
fib(n)
|
||||
{ return n < 0 ? "error" : n < 2 ? 1 : n * fib(n-1)
|
||||
}
|
||||
|
|
@ -1,31 +1,35 @@
|
|||
#import system.
|
||||
#import extensions.
|
||||
import extensions.
|
||||
|
||||
#class(extension)mathOp
|
||||
extension mathOp
|
||||
{
|
||||
#method fib
|
||||
fib
|
||||
[
|
||||
(self < 0)
|
||||
? [ #throw InvalidArgumentException new &message:"Must be non negative". ].
|
||||
if (self < 0)
|
||||
[ InvalidArgumentException new:"Must be non negative"; raise ].
|
||||
|
||||
^ control eval:self &for:
|
||||
^ control do:self with
|
||||
(:n)
|
||||
[
|
||||
(n > 1) ? [ this eval:(n - 2) + this eval:(n - 1) ] ! [ n ]
|
||||
if (n > 1)
|
||||
[ ^ $closure eval(n - 2) + $closure eval(n - 1) ];
|
||||
[ ^ n ]
|
||||
].
|
||||
]
|
||||
}
|
||||
|
||||
#symbol program =
|
||||
program =
|
||||
[
|
||||
-1 to:10 &doEach: (:i)
|
||||
-1 to:10 do(:i)
|
||||
[
|
||||
console writeLiteral:"fib(":i:")=".
|
||||
console print("fib(",i,")=").
|
||||
|
||||
console writeLine:(i fib) | if &InvalidArgumentError: e
|
||||
try (console printLine(i fib))
|
||||
{
|
||||
on(Exception e)
|
||||
[
|
||||
console writeLine:"invalid".
|
||||
].
|
||||
console printLine:"invalid"
|
||||
]
|
||||
}.
|
||||
].
|
||||
|
||||
console readChar.
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
public static long fib(int n)
|
||||
{
|
||||
if (n < 0)
|
||||
throw new IllegalArgumentException("n can not be a negative number");
|
||||
return new Object() {
|
||||
private long fibInner(int n)
|
||||
{ return (n < 2) ? n : (fibInner(n - 1) + fibInner(n - 2)); }
|
||||
}.fibInner(n);
|
||||
public static long fib(int n) {
|
||||
if (n < 0)
|
||||
throw new IllegalArgumentException("n can not be a negative number");
|
||||
|
||||
return new Object() {
|
||||
private long fibInner(int n) {
|
||||
return (n < 2) ? n : (fibInner(n - 1) + fibInner(n - 2));
|
||||
}
|
||||
}.fibInner(n);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,23 +2,24 @@ import java.util.function.Function;
|
|||
|
||||
@FunctionalInterface
|
||||
interface SelfApplicable<OUTPUT> {
|
||||
OUTPUT apply(SelfApplicable<OUTPUT> input);
|
||||
OUTPUT apply(SelfApplicable<OUTPUT> input);
|
||||
}
|
||||
|
||||
class Utils {
|
||||
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> 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() {
|
||||
return Utils.<INPUT, OUTPUT>y().apply(Utils.<INPUT, OUTPUT>y());
|
||||
}
|
||||
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 Utils.<Integer, Long>fix().apply(
|
||||
f -> n -> (n < 2) ? n : (f.apply(n - 1) + f.apply(n - 2))
|
||||
).apply(m);
|
||||
}
|
||||
public static long fib(int m) {
|
||||
if (m < 0)
|
||||
throw new IllegalArgumentException("n can not be a negative number");
|
||||
|
||||
return Utils.<Integer, Long>fix().apply(
|
||||
f -> n -> (n < 2) ? n : (f.apply(n - 1) + f.apply(n - 2))
|
||||
).apply(m);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
Task/Anonymous-recursion/Kotlin/anonymous-recursion.kotlin
Normal file
11
Task/Anonymous-recursion/Kotlin/anonymous-recursion.kotlin
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fun fib(n: Int): Int {
|
||||
require(n >= 0)
|
||||
fun fib1(k: Int, a: Int, b: Int): Int =
|
||||
if (k == 0) a else fib1(k - 1, b, a + b)
|
||||
return fib1(n, 0, 1)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
for (i in 0..20) print("${fib(i)} ")
|
||||
println()
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
1) defining a tail-recursive function:
|
||||
{def fibo {lambda {:n}
|
||||
{{lambda {:f :n :a :b} {:f :f :n :a :b}}
|
||||
{lambda {:f :n :a :b}
|
||||
{if {< :n 0}
|
||||
then the number must be positive!
|
||||
else {if {< :n 1}
|
||||
then :a
|
||||
else {:f :f {- :n 1} {+ :a :b} :a}}}} :n 1 0}}}
|
||||
|
||||
2) testing:
|
||||
{fibo -1} -> the number must be positive!
|
||||
{fibo 0} -> 1
|
||||
{fibo 8} -> 34
|
||||
{fibo 1000} -> 7.0330367711422765e+208
|
||||
{map fibo {serie 1 20}}
|
||||
-> 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
# Using scoped function fibI inside fib
|
||||
proc fib(x): int =
|
||||
proc fibI(n): int =
|
||||
proc fib(x: int): int =
|
||||
proc fibI(n: int): int =
|
||||
if n < 2: n else: fibI(n-2) + fibI(n-1)
|
||||
if x < 0:
|
||||
raise newException(ValueError, "Invalid argument")
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
/*REXX program to show anonymous recursion (of a function or subroutine). */
|
||||
numeric digits 1e6 /*in case the user goes ka-razy. */
|
||||
numeric digits 1e6 /*in case the user goes ka-razy with X.*/
|
||||
parse arg x . /*obtain the optional argument from CL.*/
|
||||
if x=='' | x=="," then x=12 /*Not specified? Then use the default.*/
|
||||
w=length(x) /*W: used for formatting the output. */
|
||||
do j=0 to x; jj=right(j, w) /*use the argument as an upper limit.*/
|
||||
say 'fibonacci('jj") =" fib(j) /*show the Fibonacci sequence: 0 ──► x */
|
||||
end /*j*/
|
||||
do j=0 for x+1 /*use the argument as an upper limit.*/
|
||||
say 'fibonacci('right(j, w)") =" fib(j)
|
||||
end /*j*/ /* [↑] show Fibonacci sequence: 0 ──► X*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
fib: procedure; parse arg z; if z>=0 then return .(z)
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
/*REXX program to show anonymous recursion of a function or subroutine with memoization.*/
|
||||
numeric digits 1e6 /*in case the user goes ka-razy. */
|
||||
numeric digits 1e6 /*in case the user goes ka-razy with X.*/
|
||||
parse arg x . /*obtain the optional argument from CL.*/
|
||||
if x=='' | x=="," then x=12 /*Not specified? Then use the default.*/
|
||||
@.=.; @.0=0; @.1=1 /*used to implement memoization for FIB*/
|
||||
w=length(x) /*W: used for formatting the output. */
|
||||
do j=0 to x; jj=right(j, w) /*use the argument as an upper limit.*/
|
||||
say 'fibonacci('jj") =" fib(j) /*show the Fibonacci sequence: 0 ──► x */
|
||||
end /*j*/
|
||||
do j=0 for x+1 /*use the argument as an upper limit.*/
|
||||
say 'fibonacci('right(j, w)") =" fib(j)
|
||||
end /*j*/ /* [↑] show Fibonacci sequence: 0 ──► X*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
fib: procedure expose @.; arg z; if z>=0 then return .(z)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{ |i|
|
||||
func (n) {
|
||||
if (n < 0) { return };
|
||||
if (n < 0) { return NaN }
|
||||
n < 2 ? n
|
||||
: (__FUNC__(n-2) + __FUNC__(n-1));
|
||||
}(i).to_s.say;
|
||||
} * 10;
|
||||
: (__FUNC__(n-2) + __FUNC__(n-1))
|
||||
}(i).say
|
||||
} * 10
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{ |i|
|
||||
{ |n|
|
||||
if (n < 0) { return };
|
||||
if (n < 0) { return NaN }
|
||||
n < 2 ? n
|
||||
: (__BLOCK__(n-2) + __BLOCK__(n-1));
|
||||
}(i).to_s.say;
|
||||
} * 10;
|
||||
: (__BLOCK__(n-2) + __BLOCK__(n-1))
|
||||
}(i).say
|
||||
} * 10
|
||||
|
|
|
|||
9
Task/Anonymous-recursion/Zkl/anonymous-recursion.zkl
Normal file
9
Task/Anonymous-recursion/Zkl/anonymous-recursion.zkl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
fcn fib(n){
|
||||
if (n<0) throw(Exception.ValueError);
|
||||
fcn(n){
|
||||
if (n < 2) return(1);
|
||||
else return(self.fcn(n-1) + self.fcn(n-2));
|
||||
}(n);
|
||||
}
|
||||
fib(8) .println();
|
||||
fib(-8).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue