Learn/OCaml/2. Values and variables
Lesson 2 of 8
Sign in

OCaml, lesson 2

Values and variables

A program works with values: numbers, text, true/false and so on. To use a value more than once, you give it a name with let.

Each of these lines is a binding: the name on the left now stands for the value on the right. Underscores inside numbers are ignored, so 522_000 is easier to read than 522000.

Basic types

You never had to write a type above, yet OCaml knows them all. This is type inference: the compiler works out each type from the value.

TypeExamplesNotes
int42, -7,

2 exercises to try what you just read.

Exercise 1

Trip budget

Press Ctrl Enter or Check to test your code.

Exercise 2

Name tag

Press Ctrl Enter or Check to test your code.

1_000
whole numbers
float3.14, 2., -0.5numbers with a decimal point
string"hello"text, in double quotes
char'a', '!'a single character, in single quotes
booltrue, falseyes/no values

You may write a type yourself when it helps the reader. The annotation goes after a colon:

OCaml never converts between types for you. An int and a float cannot be added directly, and a number is not a string. You convert explicitly with functions such as float_of_int, string_of_int and int_of_float:

Bindings do not change

In most languages a variable is a box you can refill. An OCaml binding is not a box: once city means "Lyon", it means that forever. If you write let city = "Paris" later, you create a new binding that hides the old one. This is called shadowing. Code written earlier still sees the old value.

This sounds limiting, but it is one of OCaml's strengths: when you read a name, you know its value cannot have been changed somewhere else. Most of the time you build new values from old ones instead of changing anything. (OCaml does have mutable references for the rare cases that need them, but you will not need them in this course.)

Local bindings with let ... in

Inside an expression you write let name = value in rest. The name is visible only in rest:

This is how you create the equivalent of "local variables". When the expression ends, the names disappear.

Naming

Names start with a lowercase letter or an underscore and use snake_case: total_price, max_speed. Names that start with a capital letter are reserved for modules and constructors, which you will meet later. There is no special keyword for constants, because every binding is already constant. A top-level let with a clear name is all you need.

Further reading

  • Values and Functions
  • Basic Data Types and Pattern Matching

A road trip costs fuel_per_day for each of the days, plus a one-off snacks cost. The starter already has these bindings. Add a binding called total that computes the full cost, then print it in this form:

The program reads no input.

The starter reads a first name and a last name, one per line, and binds them to first and last. Print the name in "last, first" form, and on the next line the total number of letters in both names. Use ^ to join strings and String.length to measure them.

Sample input:

Sample output:

let city = "Lyon"
let population = 522_000
let rainy = true
let price : float = 4.50
let apples = 3
let label = "Apples: " ^ string_of_int apples   (* ^ joins two strings *)
let () =
  let width = 8 in
  let height = 5 in
  let area = width * height in
  Printf.printf "Area: %d\n" area
Total: 300
Ada
Lovelace
Lovelace, Ada
Letters: 11