14 Expressions

Expressions are the building blocks of a functional language.

Expressions have:

Values are a type of expression. All values are expressions, but not all expressions are values.

In this example, 3110 is a value. 3110 evaluates to the int 3110 (as demonstrated by - : int = 3110

Here true evaluates to a boolean

This expression evaluates to true, because the integer 3110 is greater than the integer 2110.

Here, big is a string. We can also concatenate strings:

This expression evaluates to another string

We can also do arithmetic

Wait! What happened there? OCaml gave us an error message. This might surprise you, but * is the integer multiplication operator. To multiply floats, we need to use *.. This is a language design feature, so no complaining!

There, that worked as expected :). This also applies to other arithmetic operators too!

OCaml infers types. It still checks types (unlike Python).

Compilation will fail if the compiler cannot correctly infer the types. You can also manually specify types with (e : t)

Note that this is NOT a cast. It will fail if you use it as one.