lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Mon, Aug 28, 2000 at 04:52:16PM -0300, wrote:
> How does it work in Lambda-calculus?

Ok, I am not crazy enough to try pure lambda calculus :-), but here is
how it works in functional programming languages:

When a function is bound, they get a reference associated with them
that points to the frame in which the function was created. This frame
holds all the variables that were defined in its scope, in addition to
a reference to the parent frame. So, names are just looked up
normally, and frames are garbage-collected along with other
variables. There is no need for upvalues, or anything like that.

> 	It also can be done with the debug API (BTW, I don't think it's
> a better solution):
> function anonymous(n)
>    if n < 0 then return end
>    print(n)
>    getstack(1,'f').func (n-1)
> end

Wow. Talk about weird. For the sake of additional weirdness, here is
my solution to defining functions with "static" variables,
i.e. variables that retain their values across invocations of this
function. Takers, anyone? :-) 

  local vars = {x = 0}
  function a_function_that_uses_static_vars()
    %vars.x = %vars.x + 1
    return %vars.x

==> 1

==> 2

- Christian