1. static variable
- Posted by coconut Jan 07, 2011
- 1038 views
better to start new thread...
So what I envision is like C static inside function
--fibonacci generator function fibo() static atom a=0,b=1 atom tmp tmp=b b=a+b a=tmp return b end function -- print 10 first terms of fiboacci serie for i=1 to 10 do ? fibo() end for
Here a and b should be stored and initialized as any top level variable but they are only visible inside fibo() function. This can be managed at front end. There is no need for special storage class.
Jacques
2. Re: static variable
- Posted by jaygade Jan 07, 2011
- 1038 views
It doesn't matter how it is implemented. It is still yet another storage class for users to remember.
My opinion is that it's not necessary since you can already make file-level static variables.
3. Re: static variable
- Posted by bill Jan 07, 2011
- 997 views
Another way of approaching this is through a recursion
in pseudo code
function fibonacci(int N) return int
if N <= 0 then
return 0
else if N = 1 then
return 1
else
return fib-helper(1,N,0)
end if
end
function fib-helper(int i, int N, int acc) return int
if i = N then
return acc + i
else
return fib-helper(i+1,N,acc+i)
end if
end
Static variables are probably best to avoid as their implementation is likely to be tricky (it's the fact that you go out of scope yet keep a link to that scope).
Recursion has its own problems. I don't know that Euphoria does tail-call elimination - it might.
4. Re: static variable
- Posted by mattlewis (admin) Jan 07, 2011
- 1018 views
Static variables are probably best to avoid as their implementation is likely to be tricky (it's the fact that you go out of scope yet keep a link to that scope).
In fact, I think it would be pretty straightforward to implement them (after thinking for a little bit).
Recursion has its own problems. I don't know that Euphoria does tail-call elimination - it might.
Euphoria handles recursion pretty well. And yes, it does tail call recursion.
Matt