Skip to content
let Bindings
step 1/5

Reading — step 1 of 5

Learn

~1 min readFirst Steps

let binds a name to a value. All bindings are immutable by default:

let name = "Ada"
let age = 36
let pi = 3.14159

The compiler infers the type of each binding from its value. To annotate explicitly:

let age : int = 36

let ... in ... scopes a binding to an expression:

let () =
  let x = 10 in
  let y = 20 in
  print_int (x + y);
  print_newline ()

Reading stdin: read_line () returns the next line (without newline). Convert with int_of_string, float_of_string. Both throw on failure.

let () =
  let a = int_of_string (read_line ()) in
  let b = int_of_string (read_line ()) in
  print_int (a + b);
  print_newline ()

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…