1. static variable

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

new topic     » topic index » view message » categorize

2. Re: static variable

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.

new topic     » goto parent     » topic index » view message » categorize

3. Re: static variable

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.

new topic     » goto parent     » topic index » view message » categorize

4. Re: static variable

bill said...

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).

bill said...

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

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu