DEV Community

Andrey Frolov
Andrey Frolov

Posted on

2 2

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

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay