In R programing for those coming from other languages John Cook says that
R uses lexical scoping while S-PLUS uses static scope. The difference can be subtle, particularly when using closures.
I found this odd because I have always thought lexical scoping and static scoping where synonymous.
Are there distinct attributes to lexical and static scoping, or is this a distinction that changes from community to community, person to person? If so, what are the general camps and how do I tell them apart so I can better understand someones meaning when they use these words.
Answer. Lexical scoping refers to when the location of a function's definition determines which variables you have access to. On the other hand, dynamic scoping uses the location of the function's invocation to determine which variables are available.
To sum up, in static scoping the compiler first searches in the current block, then in global variables, then in successively smaller scopes. Dynamic Scoping: With dynamic scope, a global identifier refers to the identifier associated with the most recent environment and is uncommon in modern languages.
Lexical scoping, also known as static scoping, is a convention used with many modern programming languages. It refers to setting the scope, or range of functionality, of a variable so that it may be called (referenced) from within the block of code in which it is defined.
Lexical scope means that in a nested group of functions, the inner functions have access to the variables and other resources of their parent scope. This means that the child's functions are lexically bound to the execution context of their parents. Lexical scope is sometimes also referred to as static scope.
Wikipedia (and I) agree with you that the terms "lexical scope" and "static scope" are synonymous. This Lua discussion tries to make a distinction, but notes that people don't agree as to what that distinction is. :-)
It appears to me that the attempted distinction has to do with accessing names in a different function-activation-record ("stack block", if you will) than the most-current-execution record, which mainly (only?) occurs in nested functions:
function f:
var x
function h:
var y
use(y) -- obviously, accesses y in current activation of h
use(x) -- the question is, which x does this access?
With lexical scope, the answer is "the activation of f
that called the activation of h
" and with dynamic scope it means "the most recent activation that has any variable named x
" (which might not be f
). On the other hand, if the language forbids the use of x
at all, there's no question about "which x
is this" since the answer is "error". :-) It looks as though some people use "static scope" to refer to this third case.
R official documentation also addresses differences of scope between R and S-plus: http://cran.r-project.org/doc/manuals/R-intro.html#Scope
The example given from the link can be simplified like this:
cube <- function(n) {
sq <- function() n*n
n*sq()
}
The results from S-Plus and R are different:
## first evaluation in S
S> cube(2)
Error in sq(): Object "n" not found
Dumped
S> n <- 3
S> cube(2)
[1] 18
## then the same function evaluated in R
R> cube(2)
[1] 8
I personally think the way of treating variable in R is more natural.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With