Yimai
Yimai

Reputation: 87

Understanding the lazy evaluation example in Advanced R

I'm trying to understand the lazy evaluation example in Prof Hadley Wickham's Advanced R. Question has been asked about the adder function example. My question is on the other example. Why the output to the following code is "a" "x"? Why is there an "x" there? If x is only evaluated at line [3], how can it include itself?

[1] f <- function(x = ls()) {
[2]   a <- 1
[3]   x
[4] }
[5] f()

Upvotes: 0

Views: 64

Answers (1)

MrFlick
MrFlick

Reputation: 206486

Since x is a parameter to the function, it's defined when the function is run. All parameters will show up when you run ls() inside a function. A variable can exist before it's evaluated.

Upvotes: 3

Related Questions