A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
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
|
||||
|
|
@ -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
|
||||
}
|
||||
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
|
||||
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/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]}
|
||||
4
Task/Anonymous-recursion/K/anonymous-recursion-2.k
Normal file
4
Task/Anonymous-recursion/K/anonymous-recursion-2.k
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
fib'!10
|
||||
0 1 1 2 3 5 8 13 21 34
|
||||
fib -1
|
||||
"Error Negative Number"
|
||||
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
|
||||
13
Task/Anonymous-recursion/Maple/anonymous-recursion-1.maple
Normal file
13
Task/Anonymous-recursion/Maple/anonymous-recursion-1.maple
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
Fib := proc( n :: nonnegint )
|
||||
proc( k )
|
||||
option remember; # automatically memoise
|
||||
if k = 0 then
|
||||
0
|
||||
elif k = 1 then
|
||||
1
|
||||
else
|
||||
# Recurse, anonymously
|
||||
thisproc( k - 1 ) + thisproc( k - 2 )
|
||||
end
|
||||
end( n )
|
||||
end proc:
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
> seq( Fib( i ), i = 0 .. 10 );
|
||||
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
|
||||
|
||||
> Fib( -1 );
|
||||
Error, invalid input: Fib expects its 1st argument, n, to be of type
|
||||
nonnegint, but received -1
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
check := #<0&
|
||||
fib := If[check[#],Throw["Negative Argument"],If[#<=1,1,#0[#-2]+#0[#-1]]&[#]]&
|
||||
fib /@ Range[0,10]
|
||||
|
||||
{1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
check := (Print[#];#<0)&
|
||||
fib /@ Range[0,4]
|
||||
0
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
|
||||
{1, 1, 2, 3, 5}
|
||||
Loading…
Add table
Add a link
Reference in a new issue