Assign Names to Expressions

Learn how we can assign names to expressions to create abstractions.

Any powerful programming language has to provide the means for assigning names to computations. Consider, for example, the following OCaml expression.

3.14 *. 2.5 *. 2.5

Without further context, it is not obvious what this expression tries to achieve. Now compare that with a new version where we associate those numbers with names.

Press + to interact
let pi = 3.14
let radius = 2.
let circle_area = pi *. radius *. radius
(** Printing the value of 'circle_area' to the console. *)
let _ = print_float circle_area

This code is much clearer. Anyone can recognize immediately that we are calculating the area of a circle with a radius, 2.0. Here, the let statement binds pi to 3.14 and radius to 2.0. These two names are used to calculate the circle area, which is then assigned to the new name, circle_area.

You might wonder “what’s the difference between a name declared by let binding in a functional programming language and a variable in an imperative programming language?” Despite their similar appearance, they differ fundamentally from each other. In the following lesson, we’ll look deeper into this difference.

Names as labels to memory cells

The imperative programming paradigm is centered around variables and ...