1) Create a file sum.ml
2) Write this code
open Base
open Stdio
let rec read_and_accumulate accum =
let line = In_channel.input_line In_channel.stdin in
match line with
| None -> accum
| Some x -> read_and_accumulate (accum +. Float.of_string x)
let () =
printf "Total: %F\n" (read_and_accumulate 0.)
3) create dune file
(executable
(name sum)
(libraries base stdio))
4) install Stdio
opam install Stdio
5) Use dune
to build the binary
dune build sum.ml
6) Execute the program type some number and hit CTRL+d
./_build/default/sum.exe
Top comments (0)