Jensen's Device: Difference between revisions
Content deleted Content added
added ocaml |
|||
Line 100: | Line 100: | ||
foo = runST $ do |
foo = runST $ do |
||
i <- newSTRef |
i <- newSTRef 42 -- initial value doesn't matter |
||
sum' i 1 100 $ return recip `ap` readSTRef i |
sum' i 1 100 $ return recip `ap` readSTRef i |
||
Line 106: | Line 106: | ||
</pre> |
</pre> |
||
Output: 5.187377517639621 |
Output: 5.187377517639621 |
||
=={{header|OCaml}}== |
|||
{{trans|Python}} |
|||
<pre> |
|||
let i = ref 42 (* initial value doesn't matter *) |
|||
let sum' ref_i lo hi term = |
|||
let rec aux acc i = |
|||
if i > hi then |
|||
acc |
|||
else begin |
|||
ref_i := i; |
|||
aux (acc +. term ()) (i+1) |
|||
end |
|||
in |
|||
aux 0. lo |
|||
let () = |
|||
Printf.printf "%f\n" (sum' i 1 100 (fun () -> 1. /. float !i)) |
|||
</pre> |
|||
Output: 5.187378 |
|||
=={{header|Python}}== |
=={{header|Python}}== |