Anonymous user
Accumulator factory: Difference between revisions
added ocaml
(added perl) |
(added ocaml) |
||
Line 36:
}
8.3</pre>
=={{header|OCaml}}==
{{trans|Ruby}}
<lang ocaml>let accumulator sum0 =
let sum = ref sum0 in
fun n ->
sum := !sum +. n;
!sum
let () =
let x = accumulator 1.0 in
x 5.0;
accumulator 3.0; # we can't print this because you can't print a function
# also it generates a warning because we are discarding a non-unit value
Printf.printf "%f\n" (x 2.3)
;;</lang>
outputs
<pre>8.300000</pre>
=={{header|Perl}}==
|