Question: Why can a function evaluate a variable local to another function?

Consider this example:

    h := proc(x)
        printf("h says: %a evaluates to %a.", x, eval(x));
    end proc:
    f := proc()
        local z, g;
        g := proc(x)
            printf("g says: %a evaluates to %a.\n", x, eval(x));
        end proc;
        z := 2;
        g('z');
        h('z');
    end proc:

    > f()
    g says: z evaluates to 2.
    h says: z evaluates to 2.

I can't figure out, whats going on here. Now it comes as no surprise, of course, that g would be able to see z. After all, its definition is within the lexical scope of z. But how can h possibly see z?

When eval is called in h, does eval somehow look into the call stack, and find out it was called by h, which was in turn called by f and then inspect the local variables of f?

Could you explain, what's going on here, please?

Thank you all!

Please Wait...