3 ms·
R uses a "sort of" lexical scoping which causes some "interesting" things. Eg a symbol may be simultaneously in global and local scope within the same function,
by jampekka 9y ago
R uses a "sort of" lexical scoping which causes some "interesting" things. Eg a symbol may be simultaneously in global and local scope within the same function, which can be (ab)used to create a variable that's randomly scoped[0]. I'd say this is sort of a "dynamically lexical scope".
[0] http://andrewgelman.com/2014/01/29/stupid-r-tricks-random-scope/ http://andrewgelman.com/2014/01/29/stupid-r-tricks-random-sc...
- icebraining 9y agoAh, fair enough, that's definitively weird.
- kazinator 9y agoThe article is not convicing me that the language is obeying any poorly designed requirements. The function f contains a free reference to a variable called a. This is satisfied in some sort of global environment. Later, f is called from a function in which there is a local a bound to 100. Of course, this a which is local to that function is invisible to the free reference in f which continues to refers to the global a that contains 10. It could be there is some problem in R, but whatever that is, this article isn't exposing it in a convincing way. Same thing in Common Lisp (using CLISP): [1]> (defun f (x) (* a x)) F [2]> (setf a 10) 10 [3]> (f 3) 30 [4]> (defun g (y) (let ((a 100)) (f y))) G [5]> (g 3) 30 The surprise is supposed to be that `(g 3)` doesn't produce 100. Why should it? (It could produce 100 if the symbol a were marked for binding as a special variable; but it isn't. Thus it's just a free variable in f resolved in the global environment, and a lexical binding in g).