Perhaps you’ve never programmed. Or perhaps you know of other languages, and wish to learn others—this post will discuss only pure functional programming, so I suggest you forget everything you know and start anew. Either way, this series of articles provides a quick introduction to the functional side of Objective Caml.
Expressions
The vast majority of code you will write in Objective Caml will be expressions. An expression is usually a mathematical formula or something close to one. The simplest expression is a constant, such as:
1
This is not a very useful expression. A slightly more useful example would be using Objective Caml as a pocket calculator, with expressions like:
(2 + 4) * 3
Objective Caml runs expressions by computing their value: this is known as evaluation. The above expression would result in the integer value 18. As we discover more elements of the language, the rules for evaluation will become more complex, but the fundamental principle remains almost intact: an Objective Caml program is an expression or series of expressions, running the program means evaluating those expressions.
Variables
The first step in raising Objective Caml above the lowly pocket calculator level is the ability to give names to objects. The language construct for doing so is borrowed from mathematics, where mathematicians would say “let x be the smallest integer such that …” and refer to that integer as “x” from then on. The syntax for doing so is:
let (variable) = (expression) ;;
Use the semicolons for now—we will see later on that they are optional in some circumstances. Example of such a definition:
let x = (2 + 4) * 3 ;; x - 2
This will evaluate the expression (which yields the value 18) then bind that value to the name x. Every expression below the definition of x will know that x equals 18, so the second expression would evaluate to 16. Here, x is called a variable—a confusing name, since it does not actually vary: once it’s defined, it stays forever. Any name can be used for a variable, as long as it starts with a lowercase letter and contains only letters, numbers and underscores.
It is of course possible to define more than one variable in a program:
let x = 3 + 4 ;; let y = 2 * 5 ;; x + y
This example evaluates to 17. A normal Objective Caml program defines hundreds and even thousands of variables in order to work.
Having a thousand variables creates a risk for collision. The good news is that collisions are handled smoothly: at any point in the program only the last definition found so far counts. So, if we were to consider an example:
let x = 3 ;; let x = x + 1 ;; x + x
Line 1 defines x as 3, line 2 defines x again, this time as 3 + 1 = 4, and line three evaluates to 4 + 4 = 8.
This doesn’t solve the problem entirely, though: what if I accidentally overwrite a previously defined value, and I need that value later on?
Local Definitions
Objective Caml solves this by providing local definitions: instead of making a variable available to all lines that appear below it, the variable is only available within an expression. The syntax is:
let (variable) = (expression) in (expression)
The variable exists only within the second expression, and uses the value of the first expression. For instance:
let x = 1 ;; let y = let x = 2 in x + x ;; x + y
This example defines x as 1. Then, within the definition of y, it defines x again as 2, which makes the value of y equal to 2 + 2 = 4. However, the second definition of x is only available within the “x + x” expression, so the “x + y” expression uses the original value, and the result is “1 + 4 = 5″.
Local definitions are expressions. This means that they can be used as part of other expressions, such as on either side of a mathematical operator, or within other definitions. It’s perfectly legal to write code like:
1 + (let x = 1 + 2 in x * 2)
This evaluates to 1 + (3 * 2) = 7.
In practice, most variables are defined locally, and only the most important variables are defined globally.
Functions
While useful, the above features still don’t get very far beyond basic calculation needs. The one feature that turns Objective Caml into a highly expressive tool is functions. A function follows the mathematical tradition of being a mapping: it associates every element of a set with an element from another set. The element from the first set is the argument, and the element from the other set is the return value.
To call a function, you provide it with an argument and it is automatically turned into its return value for that argument. For example, the function “string_of_int” returns a bit of text representing an integer, so you could use it like so:
string_of_int 10
This would evaluate to the text “10″.
How do we define functions? Well, we simply write an expression which uses the argument and evaluates to the return value. Of course, the function doesn’t know the value of its argument until it’s called. So, when we write the function’s code code, we use a placeholder name to represent the argument: this variable is called the parameter, and it is replaced with the argument itself when the function is called. The syntax is:
fun (parameter) -> (expression)
For instance, a function that adds two to a number can be defined as:
fun x -> x + 2
It is not very useful as such, so let’s give it a name and call it:
let add = fun x -> x + 2 ;; add 3 * add 4
This evaluates to 5 * 6 = 30, and illustrates the main point of functions: they allow you to describe an operation once, and use it in many places simply by using the function’s name.
The Objective Caml language provides two shortcuts for defining functions. The first was designed to write functions that returns functions. Suppose that I write:
fun x -> (fun y -> x + y)
This can be elegantly shortened to:
fun x y -> x + y
The second shortcut was designed to make the definition of named functions easier. Suppose that I write:
let add = fun x -> x + 2
This can be elegantly shortened to:
let add x = x + 2
The two shortcuts can be combined, so that:
let add x y = x + y
Means the same as:
let add = fun x -> (fun y -> x+y)
You can see all the above (along with the general structure of the tutorial) on this page.
Hi. I'm Victor Nicollet,
Recent Comments