Jensen's Device: Difference between revisions
Content deleted Content added
restore Haskell implementation: a list comprehension is certainly more idiomatic, but it isn't call-by-name |
added standard ml |
||
Line 252: | Line 252: | ||
# it's almost as good as sum(1/i for i in range(1,101)) |
# it's almost as good as sum(1/i for i in range(1,101)) |
||
print (sum (i, 1, 100, lambda: 1.0/i.value))</lang> |
print (sum (i, 1, 100, lambda: 1.0/i.value))</lang> |
||
Output: 5.18737751764 |
|||
=={{header|Standard ML}}== |
|||
<lang sml>val i = ref 42 (* initial value doesn't matter *) |
|||
fun sum' (i, lo, hi, term) = let |
|||
val result = ref 0.0 |
|||
in |
|||
i := lo; |
|||
while !i <= hi do ( |
|||
result := !result + term (); |
|||
i := !i + 1 |
|||
); |
|||
!result |
|||
end |
|||
val () = |
|||
print (Real.toString (sum' (i, 1, 100, fn () => 1.0 / real (!i))) ^ "\n")</lang> |
|||
Output: 5.18737751764 |
Output: 5.18737751764 |