The primary piece of OCaml syntax is the expression. Just like
programs in imperative languages are primarily built out of commands,
programs in functional languages are primarily built out of expressions.
Examples of expressions include
The OCaml manual has a complete definition of all the expressions in the language. Though that page starts with a rather cryptic overview, if you scroll down, you'll come to some English explanations. Don't worry about studying that page now; just know that it's available for reference.
The primary task of computation in a functional language is to
evaluate an expression to a value. A value is an expression for
which there is no computation remaining to be performed. So, all values
are expressions, but not all expressions are values. Examples of values
The OCaml manual also has a definition of all the values, though again, that page is mostly useful for reference rather than study.
Sometimes an expression might fail to evaluate to a value. There are two reasons that might happen:
- Evaluation of the expression raises an exception.
- Evaluation of the expression never terminates (e.g., it enters an "infinite loop").
assert e evaluates
e. If the result is
more happens, and the entire expression evaluates to a special value called
unit. The unit value is written
() and its type is
But if the result is
false, an exception is raised.