Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Anonymous-recursion/00-META.yaml
Normal file
3
Task/Anonymous-recursion/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Anonymous_recursion
|
||||
note: Recursion
|
||||
19
Task/Anonymous-recursion/00-TASK.txt
Normal file
19
Task/Anonymous-recursion/00-TASK.txt
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
While implementing a recursive function, it often happens that we must resort to a separate ''helper function'' to handle the actual recursion.
|
||||
|
||||
This is usually the case when directly calling the current function would waste too many resources (stack space, execution time), causing unwanted side-effects, and/or the function doesn't have the right arguments and/or return values.
|
||||
|
||||
So we end up inventing some silly name like '''foo2''' or '''foo_helper'''. I have always found it painful to come up with a proper name, and see some disadvantages:
|
||||
|
||||
::* You have to think up a name, which then pollutes the namespace
|
||||
::* Function is created which is called from nowhere else
|
||||
::* The program flow in the source code is interrupted
|
||||
|
||||
Some languages allow you to embed recursion directly in-place. This might work via a label, a local ''gosub'' instruction, or some special keyword.
|
||||
|
||||
Anonymous recursion can also be accomplished using the [[Y combinator]].
|
||||
|
||||
|
||||
;Task:
|
||||
If possible, demonstrate this by writing the recursive version of the fibonacci function (see [[Fibonacci sequence]]) which checks for a negative argument before doing the actual recursion.
|
||||
<br><br>
|
||||
|
||||
9
Task/Anonymous-recursion/11l/anonymous-recursion.11l
Normal file
9
Task/Anonymous-recursion/11l/anonymous-recursion.11l
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
F fib(n)
|
||||
F f(Int n) -> Int
|
||||
I n < 2
|
||||
R n
|
||||
R @f(n - 1) + @f(n - 2)
|
||||
R f(n)
|
||||
|
||||
L(i) 0..20
|
||||
print(fib(i), end' ‘ ’)
|
||||
15
Task/Anonymous-recursion/ALGOL-68/anonymous-recursion.alg
Normal file
15
Task/Anonymous-recursion/ALGOL-68/anonymous-recursion.alg
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
PROC fibonacci = ( INT x )INT:
|
||||
IF x < 0
|
||||
THEN
|
||||
print( ( "negative parameter to fibonacci", newline ) );
|
||||
stop
|
||||
ELSE
|
||||
PROC actual fibonacci = ( INT n )INT:
|
||||
IF n < 2
|
||||
THEN
|
||||
n
|
||||
ELSE
|
||||
actual fibonacci( n - 1 ) + actual fibonacci( n - 2 )
|
||||
FI;
|
||||
actual fibonacci( x )
|
||||
FI;
|
||||
7
Task/Anonymous-recursion/APL/anonymous-recursion.apl
Normal file
7
Task/Anonymous-recursion/APL/anonymous-recursion.apl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fib←{ ⍝ Outer function
|
||||
⍵<0:⎕SIGNAL 11 ⍝ DOMAIN ERROR if argument < 0
|
||||
{ ⍝ Inner (anonymous) function
|
||||
⍵<2:⍵
|
||||
(∇⍵-1)+∇⍵-2 ⍝ ∇ = anonymous recursive call
|
||||
}⍵ ⍝ Call function in place
|
||||
}
|
||||
16
Task/Anonymous-recursion/Ada/anonymous-recursion.ada
Normal file
16
Task/Anonymous-recursion/Ada/anonymous-recursion.ada
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
function Fib (X: in Integer) return Integer is
|
||||
function Actual_Fib (N: in Integer) return Integer is
|
||||
begin
|
||||
if N < 2 then
|
||||
return N;
|
||||
else
|
||||
return Actual_Fib (N-1) + Actual_Fib (N-2);
|
||||
end if;
|
||||
end Actual_Fib;
|
||||
begin
|
||||
if X < 0 then
|
||||
raise Constraint_Error;
|
||||
else
|
||||
return Actual_Fib (X);
|
||||
end if;
|
||||
end Fib;
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
on fibonacci(n) -- "Anonymous recursion" task.
|
||||
-- For the sake of the task, a needlessly anonymous local script object containing a needlessly recursive handler.
|
||||
-- The script could easily (and ideally should) be assigned to a local variable.
|
||||
script
|
||||
property one : 1
|
||||
property sequence : {}
|
||||
|
||||
on f(n)
|
||||
if (n < 2) then
|
||||
set end of my sequence to 0
|
||||
if (n is 1) then set end of my sequence to one
|
||||
else
|
||||
f(n - 1)
|
||||
set end of my sequence to (item -2 of my sequence) + (end of my sequence)
|
||||
end if
|
||||
end f
|
||||
end script
|
||||
|
||||
-- Don't insert any additional code here!
|
||||
|
||||
-- Sort out whether the input's positive or negative and tell the object generated above to do the recursive business.
|
||||
tell result
|
||||
if (n < 0) then
|
||||
set its one to -1
|
||||
set n to -n
|
||||
end if
|
||||
f(n)
|
||||
|
||||
return its sequence
|
||||
end tell
|
||||
end fibonacci
|
||||
|
||||
fibonacci(15) --> {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610}
|
||||
fibonacci(-15) --> {0, -1, -1, -2, -3, -5, -8, -13, -21, -34, -55, -89, -144, -233, -377, -610}
|
||||
|
|
@ -0,0 +1,153 @@
|
|||
------------ ANONYMOUS RECURSION WITH THE Y-COMBINATOR --------
|
||||
on run
|
||||
|
||||
--------------------- FIBONACCI EXAMPLE -------------------
|
||||
|
||||
script
|
||||
on |λ|(f)
|
||||
script
|
||||
on |λ|(n)
|
||||
if 0 > n then return missing value
|
||||
if 0 = n then return 0
|
||||
if 1 = n then return 1
|
||||
(f's |λ|(n - 2)) + (f's |λ|(n - 1))
|
||||
end |λ|
|
||||
end script
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
unlines(map(showList, chunksOf(12, ¬
|
||||
map(|Y|(result), enumFromTo(-2, 20)))))
|
||||
end run
|
||||
|
||||
|
||||
------------------------ Y COMBINATOR ----------------------
|
||||
|
||||
on |Y|(f)
|
||||
script
|
||||
on |λ|(y)
|
||||
script
|
||||
on |λ|(x)
|
||||
y's |λ|(y)'s |λ|(x)
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
f's |λ|(result)
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
result's |λ|(result)
|
||||
end |Y|
|
||||
|
||||
|
||||
----------- GENERIC FUNCTIONS FOR TEST AND DISPLAY ---------
|
||||
|
||||
-- chunksOf :: Int -> [a] -> [[a]]
|
||||
on chunksOf(k, xs)
|
||||
script
|
||||
on go(ys)
|
||||
set ab to splitAt(k, ys)
|
||||
set a to item 1 of ab
|
||||
if {} ≠ a then
|
||||
{a} & go(item 2 of ab)
|
||||
else
|
||||
a
|
||||
end if
|
||||
end go
|
||||
end script
|
||||
result's go(xs)
|
||||
end chunksOf
|
||||
|
||||
|
||||
-- enumFromTo :: Int -> Int -> [Int]
|
||||
on enumFromTo(m, n)
|
||||
if n < m then
|
||||
set d to -1
|
||||
else
|
||||
set d to 1
|
||||
end if
|
||||
set lst to {}
|
||||
repeat with i from m to n by d
|
||||
set end of lst to i
|
||||
end repeat
|
||||
return lst
|
||||
end enumFromTo
|
||||
|
||||
|
||||
-- intercalate :: String -> [String] -> String
|
||||
on intercalate(delim, xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, delim}
|
||||
set s to xs as text
|
||||
set my text item delimiters to dlm
|
||||
s
|
||||
end intercalate
|
||||
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
|
||||
-- showList :: [a] -> String
|
||||
on showList(xs)
|
||||
intercalate(", ", map(my str, xs))
|
||||
end showList
|
||||
|
||||
|
||||
-- splitAt :: Int -> [a] -> ([a], [a])
|
||||
on splitAt(n, xs)
|
||||
if n > 0 and n < length of xs then
|
||||
if class of xs is text then
|
||||
{items 1 thru n of xs as text, ¬
|
||||
items (n + 1) thru -1 of xs as text}
|
||||
else
|
||||
{items 1 thru n of xs, items (n + 1) thru -1 of xs}
|
||||
end if
|
||||
else
|
||||
if n < 1 then
|
||||
{{}, xs}
|
||||
else
|
||||
{xs, {}}
|
||||
end if
|
||||
end if
|
||||
end splitAt
|
||||
|
||||
|
||||
-- str :: a -> String
|
||||
on str(x)
|
||||
x as string
|
||||
end str
|
||||
|
||||
|
||||
-- unlines :: [String] -> String
|
||||
on unlines(xs)
|
||||
-- A single string formed by the intercalation
|
||||
-- of a list of strings with the newline character.
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, linefeed}
|
||||
set s to xs as text
|
||||
set my text item delimiters to dlm
|
||||
s
|
||||
end unlines
|
||||
12
Task/Anonymous-recursion/Arturo/anonymous-recursion.arturo
Normal file
12
Task/Anonymous-recursion/Arturo/anonymous-recursion.arturo
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fib: function [x][
|
||||
; Using scoped function fibI inside fib
|
||||
fibI: function [n][
|
||||
(n<2)? -> n -> add fibI n-2 fibI n-1
|
||||
]
|
||||
if x < 0 -> panic "Invalid argument"
|
||||
return fibI x
|
||||
]
|
||||
|
||||
loop 0..4 'x [
|
||||
print fib x
|
||||
]
|
||||
23
Task/Anonymous-recursion/AutoHotkey/anonymous-recursion.ahk
Normal file
23
Task/Anonymous-recursion/AutoHotkey/anonymous-recursion.ahk
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
Fib(n) {
|
||||
nold1 := 1
|
||||
nold2 := 0
|
||||
If n < 0
|
||||
{
|
||||
MsgBox, Positive argument required!
|
||||
Return
|
||||
}
|
||||
Else If n = 0
|
||||
Return nold2
|
||||
Else If n = 1
|
||||
Return nold1
|
||||
Fib_Label:
|
||||
t := nold2+nold1
|
||||
If n > 2
|
||||
{
|
||||
n--
|
||||
nold2:=nold1
|
||||
nold1:=t
|
||||
GoSub Fib_Label
|
||||
}
|
||||
Return t
|
||||
}
|
||||
15
Task/Anonymous-recursion/AutoIt/anonymous-recursion.autoit
Normal file
15
Task/Anonymous-recursion/AutoIt/anonymous-recursion.autoit
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
ConsoleWrite(Fibonacci(10) & @CRLF) ; ## USAGE EXAMPLE
|
||||
ConsoleWrite(Fibonacci(20) & @CRLF) ; ## USAGE EXAMPLE
|
||||
ConsoleWrite(Fibonacci(30)) ; ## USAGE EXAMPLE
|
||||
|
||||
Func Fibonacci($number)
|
||||
|
||||
If $number < 0 Then Return "Invalid argument" ; No negative numbers
|
||||
|
||||
If $number < 2 Then ; If $number equals 0 or 1
|
||||
Return $number ; then return that $number
|
||||
Else ; Else $number equals 2 or more
|
||||
Return Fibonacci($number - 1) + Fibonacci($number - 2) ; FIBONACCI!
|
||||
EndIf
|
||||
|
||||
EndFunc
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
#include "axiom"
|
||||
Z ==> Integer;
|
||||
fib(x:Z):Z == {
|
||||
x <= 0 => error "argument outside of range";
|
||||
f(n:Z,v1:Z,v2:Z):Z == if n<2 then v2 else f(n-1,v2,v1+v2);
|
||||
f(x,1,1);
|
||||
}
|
||||
10
Task/Anonymous-recursion/Axiom/anonymous-recursion-2.axiom
Normal file
10
Task/Anonymous-recursion/Axiom/anonymous-recursion-2.axiom
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
)abbrev package TESTP TestPackage
|
||||
Z ==> Integer
|
||||
TestPackage : with
|
||||
fib : Z -> Z
|
||||
== add
|
||||
fib x ==
|
||||
x <= 0 => error "argument outside of range"
|
||||
f : Reference((Z,Z,Z) -> Z) := ref((n, v1, v2) +-> 0)
|
||||
f() := (n, v1, v2) +-> if n<2 then v2 else f()(n-1,v2,v1+v2)
|
||||
f()(x,1,1)
|
||||
18
Task/Anonymous-recursion/BASIC256/anonymous-recursion.basic
Normal file
18
Task/Anonymous-recursion/BASIC256/anonymous-recursion.basic
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
print Fibonacci(20)
|
||||
print Fibonacci(30)
|
||||
print Fibonacci(-10)
|
||||
print Fibonacci(10)
|
||||
end
|
||||
|
||||
function Fibonacci(num)
|
||||
if num < 0 then
|
||||
print "Invalid argument: ";
|
||||
return num
|
||||
end if
|
||||
|
||||
if num < 2 then
|
||||
return num
|
||||
else
|
||||
return Fibonacci(num - 1) + Fibonacci(num - 2)
|
||||
end If
|
||||
end function
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
PRINT FNfib(10)
|
||||
END
|
||||
|
||||
DEF FNfib(n%) IF n%<0 THEN ERROR 100, "Must not be negative"
|
||||
LOCAL P% : P% = !384 + LEN$!384 + 4 : REM Function pointer
|
||||
(n%) IF n%<2 THEN = n% ELSE = FN(^P%)(n%-1) + FN(^P%)(n%-2)
|
||||
3
Task/Anonymous-recursion/BQN/anonymous-recursion-1.bqn
Normal file
3
Task/Anonymous-recursion/BQN/anonymous-recursion-1.bqn
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
(𝕩<2)◶⟨+´𝕊¨,𝕏⟩𝕩-1‿2
|
||||
}¨↕10
|
||||
1
Task/Anonymous-recursion/BQN/anonymous-recursion-2.bqn
Normal file
1
Task/Anonymous-recursion/BQN/anonymous-recursion-2.bqn
Normal file
|
|
@ -0,0 +1 @@
|
|||
⟨ 0 1 1 2 3 5 8 13 21 34 ⟩
|
||||
3
Task/Anonymous-recursion/BQN/anonymous-recursion-3.bqn
Normal file
3
Task/Anonymous-recursion/BQN/anonymous-recursion-3.bqn
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{Fact 𝕩:
|
||||
(𝕩<2)◶⟨+´Fact¨,𝕏⟩𝕩-1‿2
|
||||
}¨↕10
|
||||
37
Task/Anonymous-recursion/BaCon/anonymous-recursion.bacon
Normal file
37
Task/Anonymous-recursion/BaCon/anonymous-recursion.bacon
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
DEF FN fib(x) = FIB(x)
|
||||
|
||||
'============================
|
||||
FUNCTION FIB(int n) TYPE int
|
||||
'============================
|
||||
|
||||
IF n < 2 THEN
|
||||
PRINT n
|
||||
|
||||
ELSE
|
||||
n1 = 0
|
||||
n2 = 1
|
||||
FOR i = 1 TO n
|
||||
sum = n1 + n2
|
||||
n1 = n2
|
||||
n2 = sum
|
||||
NEXT
|
||||
PRINT n1
|
||||
END IF
|
||||
END FUNCTION
|
||||
|
||||
'--- less than 2
|
||||
FIB(0)
|
||||
FIB(1)
|
||||
|
||||
'--- greater than or equal to 2
|
||||
FIB(2)
|
||||
FIB(3)
|
||||
FIB(4)
|
||||
FIB(5)
|
||||
FIB(6)
|
||||
FIB(7)
|
||||
FIB(8)
|
||||
FIB(9)
|
||||
|
||||
'--- using an alias
|
||||
'fib(9)
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
( (
|
||||
=
|
||||
. !arg:#:~<0
|
||||
& ( (=.!arg$!arg)
|
||||
$ (
|
||||
=
|
||||
.
|
||||
' (
|
||||
. !arg:<2
|
||||
| (($arg)$($arg))$(!arg+-2)
|
||||
+ (($arg)$($arg))$(!arg+-1)
|
||||
)
|
||||
)
|
||||
)
|
||||
$ !arg
|
||||
)
|
||||
$ 30
|
||||
)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
( /(
|
||||
' ( x
|
||||
. $x:#:~<0
|
||||
& ( /('(f.($f)$($f)))
|
||||
$ /(
|
||||
' ( r
|
||||
. /(
|
||||
' ( n
|
||||
. $n:<2
|
||||
| (($r)$($r))$($n+-2)
|
||||
+ (($r)$($r))$($n+-1)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
$ ($x)
|
||||
)
|
||||
)
|
||||
$ 30
|
||||
)
|
||||
26
Task/Anonymous-recursion/C++/anonymous-recursion-1.cpp
Normal file
26
Task/Anonymous-recursion/C++/anonymous-recursion-1.cpp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
double fib(double n)
|
||||
{
|
||||
if(n < 0)
|
||||
{
|
||||
throw "Invalid argument passed to fib";
|
||||
}
|
||||
else
|
||||
{
|
||||
struct actual_fib
|
||||
{
|
||||
static double calc(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);
|
||||
}
|
||||
26
Task/Anonymous-recursion/C++/anonymous-recursion-3.cpp
Normal file
26
Task/Anonymous-recursion/C++/anonymous-recursion-3.cpp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
double fib(double n)
|
||||
{
|
||||
if(n < 0)
|
||||
{
|
||||
throw "Invalid argument passed to fib";
|
||||
}
|
||||
else
|
||||
{
|
||||
struct
|
||||
{
|
||||
double operator()(double n)
|
||||
{
|
||||
if(n < 2)
|
||||
{
|
||||
return n;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (*this)(n-1) + (*this)(n-2);
|
||||
}
|
||||
}
|
||||
} actual_fib;
|
||||
|
||||
return actual_fib(n);
|
||||
}
|
||||
}
|
||||
8
Task/Anonymous-recursion/C-sharp/anonymous-recursion.cs
Normal file
8
Task/Anonymous-recursion/C-sharp/anonymous-recursion.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
static int Fib(int n)
|
||||
{
|
||||
if (n < 0) throw new ArgumentException("Must be non negativ", "n");
|
||||
|
||||
Func<int, int> fib = null; // Must be known, before we can assign recursively to it.
|
||||
fib = p => p > 1 ? fib(p - 2) + fib(p - 1) : p;
|
||||
return fib(n);
|
||||
}
|
||||
29
Task/Anonymous-recursion/C/anonymous-recursion-1.c
Normal file
29
Task/Anonymous-recursion/C/anonymous-recursion-1.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include <stdio.h>
|
||||
|
||||
long fib(long x)
|
||||
{
|
||||
long fib_i(long n) { return n < 2 ? n : fib_i(n - 2) + fib_i(n - 1); };
|
||||
if (x < 0) {
|
||||
printf("Bad argument: fib(%ld)\n", x);
|
||||
return -1;
|
||||
}
|
||||
return fib_i(x);
|
||||
}
|
||||
|
||||
long fib_i(long n) /* just to show the fib_i() inside fib() has no bearing outside it */
|
||||
{
|
||||
printf("This is not the fib you are looking for\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
long x;
|
||||
for (x = -1; x < 4; x ++)
|
||||
printf("fib %ld = %ld\n", x, fib(x));
|
||||
|
||||
printf("calling fib_i from outside fib:\n");
|
||||
fib_i(3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
13
Task/Anonymous-recursion/C/anonymous-recursion-2.c
Normal file
13
Task/Anonymous-recursion/C/anonymous-recursion-2.c
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#include <stdio.h>
|
||||
int main(){
|
||||
int n = 3;
|
||||
printf("%d",({
|
||||
int fib(int n){
|
||||
if (n <= 1)
|
||||
return n;
|
||||
return fib(n-1) + fib(n-2);
|
||||
}
|
||||
fib(n);
|
||||
}));
|
||||
return 0;
|
||||
}
|
||||
3
Task/Anonymous-recursion/Clio/anonymous-recursion.clio
Normal file
3
Task/Anonymous-recursion/Clio/anonymous-recursion.clio
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
10 -> (@eager) fn n:
|
||||
if n:
|
||||
n - 1 -> print -> recall
|
||||
7
Task/Anonymous-recursion/Clojure/anonymous-recursion.clj
Normal file
7
Task/Anonymous-recursion/Clojure/anonymous-recursion.clj
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(defn fib [n]
|
||||
(when (neg? n)
|
||||
(throw (new IllegalArgumentException "n should be > 0")))
|
||||
(loop [n n, v1 1, v2 1]
|
||||
(if (< n 2)
|
||||
v2
|
||||
(recur (dec n) v2 (+ v1 v2)))))
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
# This is a rather obscure technique to have an anonymous
|
||||
# function call itself.
|
||||
fibonacci = (n) ->
|
||||
throw "Argument cannot be negative" if n < 0
|
||||
do (n) ->
|
||||
return n if n <= 1
|
||||
arguments.callee(n-2) + arguments.callee(n-1)
|
||||
|
||||
# Since it's pretty lightweight to assign an anonymous
|
||||
# function to a local variable, the idiom below might be
|
||||
# more preferred.
|
||||
fibonacci2 = (n) ->
|
||||
throw "Argument cannot be negative" if n < 0
|
||||
recurse = (n) ->
|
||||
return n if n <= 1
|
||||
recurse(n-2) + recurse(n-1)
|
||||
recurse(n)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(defmacro alambda (parms &body body)
|
||||
`(labels ((self ,parms ,@body))
|
||||
#'self))
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
(defun fib (n)
|
||||
(assert (>= n 0) nil "'~a' is a negative number" n)
|
||||
(funcall
|
||||
(alambda (n)
|
||||
(if (>= 1 n)
|
||||
n
|
||||
(+ (self (- n 1)) (self (- n 2)))))
|
||||
n))
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
(defun fib (number)
|
||||
"Fibonacci sequence function."
|
||||
(if (< number 0)
|
||||
(error "Error. The number entered: ~A is negative" number)
|
||||
(labels ((fib (n a b)
|
||||
(if (= n 0)
|
||||
a
|
||||
(fib (- n 1) b (+ a b)))))
|
||||
(fib 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))))))
|
||||
14
Task/Anonymous-recursion/D/anonymous-recursion-1.d
Normal file
14
Task/Anonymous-recursion/D/anonymous-recursion-1.d
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
int fib(in uint arg) pure nothrow @safe @nogc {
|
||||
assert(arg >= 0);
|
||||
|
||||
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;
|
||||
}
|
||||
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));
|
||||
}
|
||||
33
Task/Anonymous-recursion/Delphi/anonymous-recursion.delphi
Normal file
33
Task/Anonymous-recursion/Delphi/anonymous-recursion.delphi
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
program AnonymousRecursion;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils;
|
||||
|
||||
function Fib(X: Integer): integer;
|
||||
|
||||
function DoFib(N: Integer): Integer;
|
||||
begin
|
||||
if N < 2 then Result:=N
|
||||
else Result:=DoFib(N-1) + DoFib(N-2);
|
||||
end;
|
||||
|
||||
begin
|
||||
if X < 0 then raise Exception.Create('Argument < 0')
|
||||
else Result:=DoFib(X);
|
||||
end;
|
||||
|
||||
|
||||
var I: integer;
|
||||
|
||||
begin
|
||||
for I:=-1 to 15 do
|
||||
begin
|
||||
try
|
||||
WriteLn(I:3,' - ',Fib(I):3);
|
||||
except WriteLn(I,' - Error'); end;
|
||||
end;
|
||||
WriteLn('Hit Any Key');
|
||||
ReadLn;
|
||||
end.
|
||||
13
Task/Anonymous-recursion/Dylan/anonymous-recursion.dylan
Normal file
13
Task/Anonymous-recursion/Dylan/anonymous-recursion.dylan
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
define function fib (n)
|
||||
when (n < 0)
|
||||
error("Can't take fibonacci of negative integer: %d\n", n)
|
||||
end;
|
||||
local method fib1 (n, a, b)
|
||||
if (n = 0)
|
||||
a
|
||||
else
|
||||
fib1(n - 1, b, a + b)
|
||||
end
|
||||
end;
|
||||
fib1(n, 0, 1)
|
||||
end
|
||||
17
Task/Anonymous-recursion/EMal/anonymous-recursion.emal
Normal file
17
Task/Anonymous-recursion/EMal/anonymous-recursion.emal
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
fun fibonacci = int by int n
|
||||
if n < 0 do
|
||||
logLine("Invalid argument: " + n) # logs on standard error
|
||||
return -1 ^| it should be better to raise an error,
|
||||
| but the task is about recursive functions
|
||||
|^
|
||||
end
|
||||
fun actualFibonacci = int by int n
|
||||
return when(n < 2, n, actualFibonacci(n - 1) + actualFibonacci(n - 2))
|
||||
end
|
||||
return actualFibonacci(n)
|
||||
end
|
||||
writeLine("F(0) = " + fibonacci(0))
|
||||
writeLine("F(20) = " + fibonacci(20))
|
||||
writeLine("F(-10) = " + fibonacci(-10))
|
||||
writeLine("F(30) = " + fibonacci(30))
|
||||
writeLine("F(10) = " + fibonacci(10))
|
||||
5
Task/Anonymous-recursion/EchoLisp/anonymous-recursion.l
Normal file
5
Task/Anonymous-recursion/EchoLisp/anonymous-recursion.l
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(define (fib n)
|
||||
(let _fib ((a 1) (b 1) (n n))
|
||||
(if
|
||||
(<= n 1) a
|
||||
(_fib b (+ a b) (1- n)))))
|
||||
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)
|
||||
37
Task/Anonymous-recursion/Elena/anonymous-recursion.elena
Normal file
37
Task/Anonymous-recursion/Elena/anonymous-recursion.elena
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import extensions;
|
||||
|
||||
fib(n)
|
||||
{
|
||||
if (n < 0)
|
||||
{ InvalidArgumentException.raise() };
|
||||
|
||||
^ (n)
|
||||
{
|
||||
if (n > 1)
|
||||
{
|
||||
^ this self(n - 2) + (this self(n - 1))
|
||||
}
|
||||
else
|
||||
{
|
||||
^ n
|
||||
}
|
||||
}(n)
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
for (int i := -1, i <= 10, i += 1)
|
||||
{
|
||||
console.print("fib(",i,")=");
|
||||
try
|
||||
{
|
||||
console.printLine(fib(i))
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
console.printLine:"invalid"
|
||||
}
|
||||
};
|
||||
|
||||
console.readChar()
|
||||
}
|
||||
13
Task/Anonymous-recursion/Elixir/anonymous-recursion.elixir
Normal file
13
Task/Anonymous-recursion/Elixir/anonymous-recursion.elixir
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
fib = fn f -> (
|
||||
fn x -> if x == 0, do: 0, else: (if x == 1, do: 1, else: f.(x - 1) + f.(x - 2)) end
|
||||
)
|
||||
end
|
||||
|
||||
y = fn x -> (
|
||||
fn f -> f.(f)
|
||||
end).(
|
||||
fn g -> x.(fn z ->(g.(g)).(z) end)
|
||||
end)
|
||||
end
|
||||
|
||||
IO.inspect y.(&(fib.(&1))).(40)
|
||||
15
Task/Anonymous-recursion/Erlang/anonymous-recursion.erl
Normal file
15
Task/Anonymous-recursion/Erlang/anonymous-recursion.erl
Normal 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 ).
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
let fib = function
|
||||
| n when n < 0 -> None
|
||||
| n -> let rec fib2 = function
|
||||
| 0 | 1 -> 1
|
||||
| n -> fib2 (n-1) + fib2 (n-2)
|
||||
in Some (fib2 n)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
let rec fix f x = f (fix f) x
|
||||
|
||||
let fib = function
|
||||
| n when n < 0 -> None
|
||||
| n -> Some (fix (fun f -> (function | 0 | 1 -> 1 | n -> f (n-1) + f (n-2))) n)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
[-1..5] |> List.map fib |> printfn "%A"
|
||||
[null; Some 1; Some 1; Some 2; Some 3; Some 5; Some 8]
|
||||
22
Task/Anonymous-recursion/FBSL/anonymous-recursion.fbsl
Normal file
22
Task/Anonymous-recursion/FBSL/anonymous-recursion.fbsl
Normal 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
|
||||
12
Task/Anonymous-recursion/Factor/anonymous-recursion.factor
Normal file
12
Task/Anonymous-recursion/Factor/anonymous-recursion.factor
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
USING: kernel math ;
|
||||
IN: rosettacode.fibonacci.ar
|
||||
|
||||
: fib ( n -- m )
|
||||
dup 0 < [ "fib of negative" throw ] when
|
||||
[
|
||||
! If n < 2, then drop q, else find q(n - 1) + q(n - 2).
|
||||
[ dup 2 < ] dip swap [ drop ] [
|
||||
[ [ 1 - ] dip dup call ]
|
||||
[ [ 2 - ] dip dup call ] 2bi +
|
||||
] if
|
||||
] dup call( n q -- m ) ;
|
||||
25
Task/Anonymous-recursion/Falcon/anonymous-recursion.falcon
Normal file
25
Task/Anonymous-recursion/Falcon/anonymous-recursion.falcon
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
function fib(x)
|
||||
if x < 0
|
||||
raise ParamError(description|"Negative argument invalid", extra|"Fibbonacci sequence is undefined for negative numbers")
|
||||
else
|
||||
return (function(y)
|
||||
if y == 0
|
||||
return 0
|
||||
elif y == 1
|
||||
return 1
|
||||
else
|
||||
return fself(y-1) + fself(y-2)
|
||||
end
|
||||
end)(x)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
try
|
||||
>fib(2)
|
||||
>fib(3)
|
||||
>fib(4)
|
||||
>fib(-1)
|
||||
catch in e
|
||||
> e
|
||||
end
|
||||
7
Task/Anonymous-recursion/Forth/anonymous-recursion-1.fth
Normal file
7
Task/Anonymous-recursion/Forth/anonymous-recursion-1.fth
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
:noname ( n -- n' )
|
||||
dup 2 < ?exit
|
||||
1- dup recurse swap 1- recurse + ; ( xt )
|
||||
|
||||
: fib ( +n -- n' )
|
||||
dup 0< abort" Negative numbers don't exist."
|
||||
[ ( xt from the :NONAME above ) compile, ] ;
|
||||
5
Task/Anonymous-recursion/Forth/anonymous-recursion-2.fth
Normal file
5
Task/Anonymous-recursion/Forth/anonymous-recursion-2.fth
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
( xt from :noname in the previous example )
|
||||
variable pocket pocket !
|
||||
: fib ( +n -- n' )
|
||||
dup 0< abort" Negative numbers don't exist."
|
||||
[ pocket @ compile, ] ;
|
||||
3
Task/Anonymous-recursion/Forth/anonymous-recursion-3.fth
Normal file
3
Task/Anonymous-recursion/Forth/anonymous-recursion-3.fth
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
: fib ( +n -- )
|
||||
dup 0< abort" Negative numbers don't exist"
|
||||
[: dup 2 < ?exit 1- dup MYSELF swap 1- MYSELF + ;] execute . ;
|
||||
18
Task/Anonymous-recursion/Fortran/anonymous-recursion.f
Normal file
18
Task/Anonymous-recursion/Fortran/anonymous-recursion.f
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
integer function fib(n)
|
||||
integer, intent(in) :: n
|
||||
if (n < 0 ) then
|
||||
write (*,*) 'Bad argument: fib(',n,')'
|
||||
stop
|
||||
else
|
||||
fib = purefib(n)
|
||||
end if
|
||||
contains
|
||||
recursive pure integer function purefib(n) result(f)
|
||||
integer, intent(in) :: n
|
||||
if (n < 2 ) then
|
||||
f = n
|
||||
else
|
||||
f = purefib(n-1) + purefib(n-2)
|
||||
end if
|
||||
end function purefib
|
||||
end function fib
|
||||
42
Task/Anonymous-recursion/FreeBASIC/anonymous-recursion.basic
Normal file
42
Task/Anonymous-recursion/FreeBASIC/anonymous-recursion.basic
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
#Lang "fblite"
|
||||
|
||||
Option Gosub '' enables Gosub to be used
|
||||
|
||||
' Using gosub to simulate a nested function
|
||||
Function fib(n As UInteger) As UInteger
|
||||
Gosub nestedFib
|
||||
Exit Function
|
||||
|
||||
nestedFib:
|
||||
fib = IIf(n < 2, n, fib(n - 1) + fib(n - 2))
|
||||
Return
|
||||
End Function
|
||||
|
||||
' This function simulates (rather messily) gosub by using 2 gotos and would therefore work
|
||||
' even in the default dialect
|
||||
Function fib2(n As UInteger) As UInteger
|
||||
Goto nestedFib
|
||||
|
||||
exitFib:
|
||||
Exit Function
|
||||
|
||||
nestedFib:
|
||||
fib2 = IIf(n < 2, n, fib2(n - 1) + fib2(n - 2))
|
||||
Goto exitFib
|
||||
End Function
|
||||
|
||||
For i As Integer = 1 To 12
|
||||
Print fib(i); " ";
|
||||
Next
|
||||
|
||||
Print
|
||||
|
||||
For j As Integer = 1 To 12
|
||||
Print fib2(j); " ";
|
||||
Next
|
||||
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
45
Task/Anonymous-recursion/Go/anonymous-recursion-1.go
Normal file
45
Task/Anonymous-recursion/Go/anonymous-recursion-1.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
for _, n := range []int{0, 1, 2, 3, 4, 5, 10, 40, -1} {
|
||||
f, ok := arFib(n)
|
||||
if ok {
|
||||
fmt.Printf("fib %d = %d\n", n, f)
|
||||
} else {
|
||||
fmt.Println("fib undefined for negative numbers")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func arFib(n int) (int, bool) {
|
||||
switch {
|
||||
case n < 0:
|
||||
return 0, false
|
||||
case n < 2:
|
||||
return n, true
|
||||
}
|
||||
return yc(func(recurse fn) fn {
|
||||
return func(left, term1, term2 int) int {
|
||||
if left == 0 {
|
||||
return term1+term2
|
||||
}
|
||||
return recurse(left-1, term1+term2, term1)
|
||||
}
|
||||
})(n-2, 1, 0), true
|
||||
}
|
||||
|
||||
type fn func(int, int, int) int
|
||||
type ff func(fn) fn
|
||||
type fx func(fx) fn
|
||||
|
||||
func yc(f ff) fn {
|
||||
return func(x fx) fn {
|
||||
return x(x)
|
||||
}(func(x fx) fn {
|
||||
return f(func(a1, a2, a3 int) int {
|
||||
return x(x)(a1, a2, a3)
|
||||
})
|
||||
})
|
||||
}
|
||||
34
Task/Anonymous-recursion/Go/anonymous-recursion-2.go
Normal file
34
Task/Anonymous-recursion/Go/anonymous-recursion-2.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func fib(n int) (result int, err error) {
|
||||
var fib func(int) int // Must be declared first so it can be called in the closure
|
||||
fib = func(n int) int {
|
||||
if n < 2 {
|
||||
return n
|
||||
}
|
||||
return fib(n-1) + fib(n-2)
|
||||
}
|
||||
|
||||
if n < 0 {
|
||||
err = errors.New("negative n is forbidden")
|
||||
return
|
||||
}
|
||||
|
||||
result = fib(n)
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
for i := -1; i <= 10; i++ {
|
||||
if result, err := fib(i); err != nil {
|
||||
fmt.Printf("fib(%d) returned error: %s\n", i, err)
|
||||
} else {
|
||||
fmt.Printf("fib(%d) = %d\n", i, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
def fib = {
|
||||
assert it > -1
|
||||
{i -> i < 2 ? i : {j -> owner.call(j)}(i-1) + {k -> owner.call(k)}(i-2)}(it)
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
def fib0to20 = (0..20).collect(fib)
|
||||
println fib0to20
|
||||
|
||||
try {
|
||||
println fib(-25)
|
||||
} catch (Throwable e) {
|
||||
println "KABOOM!!"
|
||||
println e.message
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
fib :: Integer -> Maybe Integer
|
||||
fib n
|
||||
| n < 0 = Nothing
|
||||
| otherwise = Just $ real n
|
||||
where real 0 = 1
|
||||
real 1 = 1
|
||||
real n = real (n-1) + real (n-2)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
import Data.Function (fix)
|
||||
|
||||
fib :: Integer -> Maybe Integer
|
||||
fib n
|
||||
| n < 0 = Nothing
|
||||
| otherwise = Just $ fix (\f -> (\n -> if n > 1 then f (n-1) + f (n-2) else 1)) n
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
ghci> map fib [-4..10]
|
||||
[Nothing,Nothing,Nothing,Nothing,Just 1,Just 1,Just 2,Just 3,Just 5,Just 8,Just 13,Just 21,Just 34,Just 55,Just 89]
|
||||
23
Task/Anonymous-recursion/Haskell/anonymous-recursion-4.hs
Normal file
23
Task/Anonymous-recursion/Haskell/anonymous-recursion-4.hs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
fib :: Integer -> Maybe Integer
|
||||
fib n
|
||||
| n < 0 = Nothing
|
||||
| otherwise =
|
||||
Just $
|
||||
(\f ->
|
||||
let x = f x
|
||||
in x)
|
||||
(\f n ->
|
||||
if n > 1
|
||||
then f (n - 1) + f (n - 2)
|
||||
else 1)
|
||||
n
|
||||
|
||||
-- TEST ----------------------------------------------------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
print $
|
||||
fib <$> [-4 .. 10] >>=
|
||||
\m ->
|
||||
case m of
|
||||
Just x -> [x]
|
||||
_ -> []
|
||||
16
Task/Anonymous-recursion/IS-BASIC/anonymous-recursion.basic
Normal file
16
Task/Anonymous-recursion/IS-BASIC/anonymous-recursion.basic
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
100 PROGRAM "Fibonacc.bas"
|
||||
110 FOR I=0 TO 10
|
||||
120 PRINT FIB(I);
|
||||
130 NEXT
|
||||
140 DEF FIB(K)
|
||||
150 SELECT CASE K
|
||||
160 CASE IS<0
|
||||
170 PRINT "Negative parameter to Fibonacci.":STOP
|
||||
180 CASE 0
|
||||
190 LET FIB=0
|
||||
200 CASE 1
|
||||
210 LET FIB=1
|
||||
220 CASE ELSE
|
||||
230 LET FIB=FIB(K-1)+FIB(K-2)
|
||||
240 END SELECT
|
||||
250 END DEF
|
||||
24
Task/Anonymous-recursion/Icon/anonymous-recursion-1.icon
Normal file
24
Task/Anonymous-recursion/Icon/anonymous-recursion-1.icon
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
procedure main(A)
|
||||
every write("fib(",a := numeric(!A),")=",fib(a))
|
||||
end
|
||||
|
||||
procedure fib(n)
|
||||
local source, i
|
||||
static cache
|
||||
initial {
|
||||
cache := table()
|
||||
cache[0] := 0
|
||||
cache[1] := 1
|
||||
}
|
||||
if type(n) == "integer" & n >= 0 then
|
||||
return n @ makeProc {{
|
||||
i := @(source := &source) # 1
|
||||
/cache[i] := ((i-1)@makeProc(^¤t)+(i-2)@makeProc(^¤t)) # 2
|
||||
cache[i] @ source # 3
|
||||
}}
|
||||
end
|
||||
|
||||
procedure makeProc(A)
|
||||
A := if type(A) == "list" then A[1]
|
||||
return (@A, A) # prime and return
|
||||
end
|
||||
9
Task/Anonymous-recursion/Icon/anonymous-recursion-2.icon
Normal file
9
Task/Anonymous-recursion/Icon/anonymous-recursion-2.icon
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
procedure fib(n)
|
||||
local source, i
|
||||
if type(n) == "integer" & n >= 0 then
|
||||
return n @ makeProc {{
|
||||
i := @(source := &source)
|
||||
if i = (0|1) then i@source
|
||||
((i-1)@makeProc(^¤t) + (i-2)@makeProc(^¤t)) @ source
|
||||
}}
|
||||
end
|
||||
7
Task/Anonymous-recursion/Io/anonymous-recursion.io
Normal file
7
Task/Anonymous-recursion/Io/anonymous-recursion.io
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fib := method(x,
|
||||
if(x < 0, Exception raise("Negative argument not allowed!"))
|
||||
fib2 := method(n,
|
||||
if(n < 2, n, fib2(n-1) + fib2(n-2))
|
||||
)
|
||||
fib2(x floor)
|
||||
)
|
||||
1
Task/Anonymous-recursion/J/anonymous-recursion-1.j
Normal file
1
Task/Anonymous-recursion/J/anonymous-recursion-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
fibN=: (-&2 +&$: -&1)^:(1&<) M."0
|
||||
4
Task/Anonymous-recursion/J/anonymous-recursion-2.j
Normal file
4
Task/Anonymous-recursion/J/anonymous-recursion-2.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
fibN 12
|
||||
144
|
||||
fibN i.31
|
||||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040
|
||||
1
Task/Anonymous-recursion/J/anonymous-recursion-3.j
Normal file
1
Task/Anonymous-recursion/J/anonymous-recursion-3.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
basis ` ($: @: g) @. test
|
||||
1
Task/Anonymous-recursion/J/anonymous-recursion-4.j
Normal file
1
Task/Anonymous-recursion/J/anonymous-recursion-4.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
basis @: (g^:test^:_)
|
||||
10
Task/Anonymous-recursion/Java/anonymous-recursion-1.java
Normal file
10
Task/Anonymous-recursion/Java/anonymous-recursion-1.java
Normal file
|
|
@ -0,0 +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);
|
||||
}
|
||||
25
Task/Anonymous-recursion/Java/anonymous-recursion-2.java
Normal file
25
Task/Anonymous-recursion/Java/anonymous-recursion-2.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import java.util.function.Function;
|
||||
|
||||
@FunctionalInterface
|
||||
interface SelfApplicable<OUTPUT> {
|
||||
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> 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
function fibo(n) {
|
||||
if (n < 0) { throw "Argument cannot be negative"; }
|
||||
|
||||
return (function(n) {
|
||||
return (n < 2) ? n : arguments.callee(n-1) + arguments.callee(n-2);
|
||||
})(n);
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
function fibo(n) {
|
||||
if (n < 0) { throw "Argument cannot be negative"; }
|
||||
|
||||
return (function fib(n) {
|
||||
return (n < 2) ? n : fib(n-1) + fib(n-2);
|
||||
})(n);
|
||||
}
|
||||
1
Task/Anonymous-recursion/Joy/anonymous-recursion.joy
Normal file
1
Task/Anonymous-recursion/Joy/anonymous-recursion.joy
Normal file
|
|
@ -0,0 +1 @@
|
|||
fib == [small] [] [pred dup pred] [+] binrec;
|
||||
1
Task/Anonymous-recursion/Jq/anonymous-recursion-1.jq
Normal file
1
Task/Anonymous-recursion/Jq/anonymous-recursion-1.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
0 | recurse(. + 1)
|
||||
8
Task/Anonymous-recursion/Jq/anonymous-recursion-2.jq
Normal file
8
Task/Anonymous-recursion/Jq/anonymous-recursion-2.jq
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
def fib(n):
|
||||
def aux: if . == 0 then 0
|
||||
elif . == 1 then 1
|
||||
else (. - 1 | aux) + (. - 2 | aux)
|
||||
end;
|
||||
if n < 0 then error("negative arguments not allowed")
|
||||
else n | aux
|
||||
end ;
|
||||
7
Task/Anonymous-recursion/Julia/anonymous-recursion.julia
Normal file
7
Task/Anonymous-recursion/Julia/anonymous-recursion.julia
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
function fib(n)
|
||||
if n < 0
|
||||
throw(ArgumentError("negative arguments not allowed"))
|
||||
end
|
||||
aux(m) = m < 2 ? one(m) : aux(m-1) + aux(m-2)
|
||||
aux(n)
|
||||
end
|
||||
1
Task/Anonymous-recursion/K/anonymous-recursion-1.k
Normal file
1
Task/Anonymous-recursion/K/anonymous-recursion-1.k
Normal file
|
|
@ -0,0 +1 @@
|
|||
fib: {:[x<0; "Error Negative Number"; {:[x<2;x;_f[x-2]+_f[x-1]]}x]}
|
||||
1
Task/Anonymous-recursion/K/anonymous-recursion-2.k
Normal file
1
Task/Anonymous-recursion/K/anonymous-recursion-2.k
Normal file
|
|
@ -0,0 +1 @@
|
|||
fib: {:[x<0; "Error Negative Number"; {:[x<2;x;o[x-2]+o[x-1]]}x]}
|
||||
4
Task/Anonymous-recursion/K/anonymous-recursion-3.k
Normal file
4
Task/Anonymous-recursion/K/anonymous-recursion-3.k
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
fib'!10
|
||||
0 1 1 2 3 5 8 13 21 34
|
||||
fib -1
|
||||
"Error Negative Number"
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
include ..\Utilitys.tlhy
|
||||
|
||||
|
||||
:fib %f !f
|
||||
%fr
|
||||
[ %n !n
|
||||
$n 2 <
|
||||
( [$n]
|
||||
[$n 1 - $fr eval $n 2 - $fr eval +] )
|
||||
if
|
||||
] !fr
|
||||
|
||||
$f 0 <
|
||||
( ["Error: number is negative"]
|
||||
[$f true $fr if] )
|
||||
if
|
||||
;
|
||||
|
||||
|
||||
25 fib ?
|
||||
msec ?
|
||||
"End " input
|
||||
1
Task/Anonymous-recursion/Klong/anonymous-recursion.klong
Normal file
1
Task/Anonymous-recursion/Klong/anonymous-recursion.klong
Normal file
|
|
@ -0,0 +1 @@
|
|||
fib::{:[x<0;"error: negative":|x<2;x;.f(x-1)+.f(x-2)]}
|
||||
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 fib(k: Int, a: Int, b: Int): Int =
|
||||
if (k == 0) a else fib(k - 1, b, a + b)
|
||||
return fib(n, 0, 1)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
for (i in 0..20) print("${fib(i)} ")
|
||||
println()
|
||||
}
|
||||
32
Task/Anonymous-recursion/LOLCODE/anonymous-recursion.lol
Normal file
32
Task/Anonymous-recursion/LOLCODE/anonymous-recursion.lol
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
HAI 1.3
|
||||
|
||||
HOW IZ I fib YR x
|
||||
DIFFRINT x AN BIGGR OF x AN 0, O RLY?
|
||||
YA RLY, FOUND YR "ERROR"
|
||||
OIC
|
||||
|
||||
HOW IZ I fib_i YR n
|
||||
DIFFRINT n AN BIGGR OF n AN 2, O RLY?
|
||||
YA RLY, FOUND YR n
|
||||
OIC
|
||||
|
||||
FOUND YR SUM OF...
|
||||
I IZ fib_i YR DIFF OF n AN 2 MKAY AN...
|
||||
I IZ fib_i YR DIFF OF n AN 1 MKAY
|
||||
IF U SAY SO
|
||||
|
||||
FOUND YR I IZ fib_i YR x MKAY
|
||||
IF U SAY SO
|
||||
|
||||
HOW IZ I fib_i YR n
|
||||
VISIBLE "SRY U CANT HAS FIBS DIS TIEM"
|
||||
IF U SAY SO
|
||||
|
||||
IM IN YR fibber UPPIN YR i TIL BOTH SAEM i AN 5
|
||||
I HAS A i ITZ DIFF OF i AN 1
|
||||
VISIBLE "fib(:{i}) = " I IZ fib YR i MKAY
|
||||
IM OUTTA YR fibber
|
||||
|
||||
I IZ fib_i YR 3 MKAY
|
||||
|
||||
KTHXBYE
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
1) defining a quasi-recursive function combined with a simple Ω-combinator:
|
||||
{def fibo {lambda {:n}
|
||||
{{{lambda {:f} {:f :f}}
|
||||
{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}}}
|
||||
-> fibo
|
||||
|
||||
2) testing:
|
||||
{fibo -1} -> the number must be positive!
|
||||
{fibo 0} -> 1
|
||||
{fibo 8} -> 34
|
||||
{fibo 1000} -> 7.0330367711422765e+208
|
||||
{S.map fibo {S.serie 1 20}}
|
||||
-> 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
|
||||
|
||||
We could also avoid any name and write an IIFE
|
||||
|
||||
{{lambda {:n}
|
||||
{{{lambda {:f} {:f :f}}
|
||||
{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}}
|
||||
8}
|
||||
-> 34
|
||||
15
Task/Anonymous-recursion/Lang/anonymous-recursion.lang
Normal file
15
Task/Anonymous-recursion/Lang/anonymous-recursion.lang
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
fp.fib = ($n) -> {
|
||||
if($n < 0) {
|
||||
throw fn.withErrorMessage($LANG_ERROR_INVALID_ARGUMENTS, n must be >= 0)
|
||||
}
|
||||
|
||||
fp.innerFib = ($n) -> {
|
||||
if($n < 2) {
|
||||
return $n
|
||||
}
|
||||
|
||||
return parser.op(fp.innerFib($n - 1) + fp.innerFib($n - 2))
|
||||
}
|
||||
|
||||
return fp.innerFib($n)
|
||||
}
|
||||
12
Task/Anonymous-recursion/Lingo/anonymous-recursion-1.lingo
Normal file
12
Task/Anonymous-recursion/Lingo/anonymous-recursion-1.lingo
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
on fib (n)
|
||||
if n<0 then return _player.alert("negative arguments not allowed")
|
||||
|
||||
-- create instance of unnamed class in memory only (does not pollute namespace)
|
||||
m = new(#script)
|
||||
r = RETURN
|
||||
m.scriptText = "on fib (me,n)"&r&"if n<2 then return n"&r&"return me.fib(n-1)+me.fib(n-2)"&r&"end"
|
||||
aux = m.script.new()
|
||||
m.erase()
|
||||
|
||||
return aux.fib(n)
|
||||
end
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
put fib(10)
|
||||
-- 55
|
||||
7
Task/Anonymous-recursion/Lua/anonymous-recursion-1.lua
Normal file
7
Task/Anonymous-recursion/Lua/anonymous-recursion-1.lua
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
local function Y(x) return (function (f) return f(f) end)(function(y) return x(function(z) return y(y)(z) end) end) end
|
||||
|
||||
return Y(function(fibs)
|
||||
return function(n)
|
||||
return n < 2 and 1 or fibs(n - 1) + fibs(n - 2)
|
||||
end
|
||||
end)
|
||||
4
Task/Anonymous-recursion/Lua/anonymous-recursion-2.lua
Normal file
4
Task/Anonymous-recursion/Lua/anonymous-recursion-2.lua
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
return setmetatable({1,1},{__index = function(self, n)
|
||||
self[n] = self[n-1] + self[n-2]
|
||||
return self[n]
|
||||
end})
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
A$={{ Module "Fibonacci" : Read X :If X<0 then {Error {X<0}} Else Fib=Lambda (x)->if(x>1->fib(x-1)+fib(x-2), x) : =fib(x)}}
|
||||
Try Ok {
|
||||
Print Function(A$, -12)
|
||||
}
|
||||
If Error or Not Ok Then Print Error$
|
||||
Print Function(A$, 12)=144 ' true
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
Function fib(x) {
|
||||
If x<0 then Error "argument outside of range"
|
||||
If x<2 then =x : exit
|
||||
Def fib1(x)=If(x>1->lambda(x-1)+lambda(x-2), x)
|
||||
=fib1(x)
|
||||
}
|
||||
Module CheckIt (&k()) {
|
||||
Print k(12)
|
||||
}
|
||||
CheckIt &Fib()
|
||||
Print fib(-2) ' error
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
fib=lambda -> {
|
||||
fib1=lambda (x)->If(x>1->lambda(x-1)+lambda(x-2), x)
|
||||
=lambda fib1 (x) -> {
|
||||
If x<0 then Error "argument outside of range"
|
||||
If x<2 then =x : exit
|
||||
=fib1(x)
|
||||
}
|
||||
}() ' using () execute this lambda so fib get the returned lambda
|
||||
Module CheckIt (&k()) {
|
||||
Print k(12)
|
||||
}
|
||||
CheckIt &Fib()
|
||||
Try {
|
||||
Print fib(-2)
|
||||
}
|
||||
Print Error$
|
||||
Z=Fib
|
||||
Print Z(12)
|
||||
Dim a(10)
|
||||
a(3)=Z
|
||||
Print a(3)(12)=144
|
||||
Inventory Alfa = "key1":=Z
|
||||
Print Alfa("key1")(12)=144
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
Class Something {
|
||||
\\ this class is a global function
|
||||
\\ return a group with a value with one parameter
|
||||
private:
|
||||
\\ we can use lambda(), but here we use .fib1() as This.fib1()
|
||||
fib1=lambda (x)->If(x>1->.fib1(x-1)+.fib1(x-2), x)
|
||||
public:
|
||||
Value (x) {
|
||||
If x<0 then Error "argument outside of range"
|
||||
If x<2 then =x : exit
|
||||
=This.fib1(x) \\ we can omit This using .fib1(x)
|
||||
}
|
||||
}
|
||||
K=Something() ' K is a static group here
|
||||
Print k(12)=144
|
||||
Dim a(10)
|
||||
a(4)=Group(K)
|
||||
Print a(4)(12)=144
|
||||
pk->Something() ' pk is a pointer to group (object in M2000)
|
||||
\\ pointers need Eval to process arguments
|
||||
Print Eval(pk, 12)=144
|
||||
Inventory Alfa = "Key2":=Group(k), 10*10:=pk
|
||||
Print Alfa("Key2")(12)=144
|
||||
Print Eval(Alfa("100"),12)=144, Eval(Alfa(100),12)=144
|
||||
11
Task/Anonymous-recursion/MATLAB/anonymous-recursion.m
Normal file
11
Task/Anonymous-recursion/MATLAB/anonymous-recursion.m
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function v = fibonacci(n)
|
||||
assert(n >= 0)
|
||||
v = fibonacci(n,0,1);
|
||||
|
||||
% nested function
|
||||
function a = fibonacci(n,a,b)
|
||||
if n ~= 0
|
||||
a = fibonacci(n-1,b,a+b);
|
||||
end
|
||||
end
|
||||
end
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue