Jensen's Device: Difference between revisions
Content deleted Content added
Added PicoLisp |
Added R code |
||
Line 505: | Line 505: | ||
print harmonic_sum(i, 1, 100, lambda: 1.0/i.value)</lang> |
print harmonic_sum(i, 1, 100, lambda: 1.0/i.value)</lang> |
||
Output: 5.18737751764 |
Output: 5.18737751764 |
||
=={{header|R}}== |
|||
R uses a [http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_need call by need] evaluation strategy where function inputs |
|||
are lazily evaluated and then cached; functions can implement |
|||
nonstandard evaluation such as call-by-name by using the "substitute" primitive to |
|||
access the parse tree of the unevaluated input. There are some proposed |
|||
[http://developer.r-project.org/nonstandard-eval.pdf conventions] to do this in a way that is less confusing to the user |
|||
of a function. The following ignores any conventions to mimic the effect |
|||
of the original ALGOL where "i" is truly assigned in the calling context. |
|||
<lang R> |
|||
sum <- function(var, lo, hi, term) { |
|||
s <- 0 |
|||
for (i in lo:hi) { |
|||
eval.parent(substitute(x <- i, list(x=substitute(var), i=i))) |
|||
s <- s + eval.parent(substitute(term)) |
|||
} |
|||
s |
|||
} |
|||
print(sum(i, 1, 100, 1/i)) |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |