RosettaCodeData/Task/Jensens-Device/R/jensens-device.r
2023-07-01 13:44:08 -04:00

15 lines
369 B
R

sum <- function(var, lo, hi, term)
eval(substitute({
.temp <- 0;
for (var in lo:hi) {
.temp <- .temp + term
}
.temp
}, as.list(match.call()[-1])),
enclos=parent.frame())
sum(i, 1, 100, 1/i) #prints 5.187378
##and because of enclos=parent.frame(), the term can involve variables in the caller's scope:
x <- -1
sum(i, 1, 100, i^x) #5.187378