Skip to content
Lesson 2 of 9

Step 1 of 5 · Reading · ~1 min

Learn

First 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 ()
Up nextDefining FunctionsFunctions

Discussion

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

Sign in to post a comment or reply.

Loading…