DEV Community

Andrey Frolov
Andrey Frolov

Posted on

Compile a first program

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.)

Enter fullscreen mode Exit fullscreen mode

3) create dune file

(executable
 (name      sum)
 (libraries base stdio))
Enter fullscreen mode Exit fullscreen mode

4) install Stdio

opam install Stdio 
Enter fullscreen mode Exit fullscreen mode

5) Use dune to build the binary

dune build sum.ml
Enter fullscreen mode Exit fullscreen mode

6) Execute the program type some number and hit CTRL+d

./_build/default/sum.exe
Enter fullscreen mode Exit fullscreen mode

Discussion (0)