Jensen's Device: Difference between revisions

added ocaml
(added ocaml)
Line 100:
 
foo = runST $ do
i <- newSTRef 042 -- initial value doesn't matter
sum' i 1 100 $ return recip `ap` readSTRef i
 
Line 106:
</pre>
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}}==
Anonymous user