RosettaCodeData/Task/Anonymous-recursion/Icon/anonymous-recursion-1.icon

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(^&current)+(i-2)@makeProc(^&current)) # 2
cache[i] @ source # 3
}}
end
procedure makeProc(A)
A := if type(A) == "list" then A[1]
return (@A, A) # prime and return
end