4 Compute on the language
R is very powerful with the ability of “Computing on the Language”(基于语言的计算) involving function, evaluation and environment.
4.1 core rules
- lazy evaluation to decide when to evaluate the parameter,
- call stack to decide where (start point) to evaluate the argument
- lexically scoping to where to find undefined symbol in the argument
4.2 lazy evaluation
function(x) {y <- 1; message('in f()'); x;}
f <-
f(message('evaluate argument'))
#> in f()
#> evaluate argument
In f()
, the parameter x
is not evaluated until it’s used, i.e., after message('in f()')
.
function(x) {message('in f()'); g(x)}
f <- function(x) {message('in g()'); x}
g <-
f(message('evaluate argument'))
#> in f()
#> in g()
#> evaluate argument
In f()
, passing the parameter x
as argument for g()
doesn’t evaluate it.
4.3 call stack
function(x) {message('in f()'); g(x)}
f <- function(x) {message('in g()'); y <- 2; x}
g <-
1
y <-f(message('y = ', y))
#> in f()
#> in g()
#> y = 1
We already know that, message('y = ', y)
is lazy evaluated after y <- 2
inside g()
. However,when the evaluation happens, the stage is NOT the execution env of g()
. Rather, we start from there, backtrack the call stack, until we arrive the start point, i.e., the caller env of f()
(the global env here).
4.4 lexically scoping
function(u) (u + v) / 2
g <- function(u) {
h <- 1
v <-g(u)
} 2
v <-h(10)
#> [1] 6
Although g()
is called inside h()
, it finds v
in where it’s defined, i.e., the global env.
Actually, lexically scoping is not a special rule, and the implemention is rather straightforward. The execution environment’s parent is function environment, i.e., when the function is created. So we just need to follow the general rule of parent environment.
4.5 technical details
- the evaluation of argument happens only once, refer to here
- call stack is implemented in that a frame’s parent is the previous frame, refer to here
- when you write a function factory, you may need to force evaluation