by civilized · 2y ago · 0 comments
If your code (or the code of the libraries you're using) doesn't use any non-standard evaluation tools, evaluation will be eager and work like any other ALGOL language.
It is possible to make some objects behave in a lazy way, but this is also true of many other languages.
To be even more precisely, R itself is lazy "all the way through". Because literally every expression in R is syntactic sugar for a function call (including assignments and control structures such as "if"), the only thing that a function can really do with an argument is pass it on to another function, so, strictly speaking, there's no distinction between use and non-use even. It's just that any R function, in order to do something useful, will ultimately call some non-R leaf function implemented in native code, and some of those leaf functions will actually do the eval if they're defined in terms of argument values (e.g. obviously addition needs to do so to actually compute the value etc).
f = function(x) { print('hello'); x }
f(print('world'))
X is not evaluated in f until referenced. Indeed if you remove x from f, world is not printed.My impression was that R was mostly an eager language that somehow allowed for laziness. I will research this further and hopefully suss out why I got confused.
[1] https://dl.acm.org/doi/10.1145/3360579
[2] https://www.r-bloggers.com/2018/07/about-lazy-evaluation/