OCaml, lesson 2
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.
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.
| Type | Examples | Notes |
|---|---|---|
int | 42, -7, |
2 exercises to try what you just read.
1_000| whole numbers |
float | 3.14, 2., -0.5 | numbers with a decimal point |
string | "hello" | text, in double quotes |
char | 'a', '!' | a single character, in single quotes |
bool | true, false | yes/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:
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.)
let ... inInside 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.
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.
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 = truelet price : float = 4.50let 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" areaTotal: 300Ada
LovelaceLovelace, Ada
Letters: 11