25 lines
660 B
Plaintext
25 lines
660 B
Plaintext
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
|