Friday, 24 July 2015

R: Lazy evaluation

In programming, Lazy evaluation delays the evaluation of an expression until its value is needed. In R functions, arguments are evaluated only when needed.


LazyEvalEx1.R
printData <- function(a, b){
 print("Value of a is")
 print(a)
 
 print("Value of b is")
 print(b)
}

printData(10)


Observe above script, I don’t pass second argument for the function printData. When I tried to run the script, it runs fine, but only throws error when it encounters b. But in other programming languages like Java, compiler itself throws error.
$ Rscript LazyEvalEx1.R 
[1] "Value of a is"
[1] 10
[1] "Value of b is"
Error in print(b) : argument "b" is missing, with no default
Calls: printData -> print
Execution halted


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment