let [var] : [type] = [expre. def] in [expre. body]
[expre.def]
is an expression defining a value of given type and [expre.body]
is an expression within which the variable can be used as the name for the defined valuelet
is often referred to as a binding construct, the name being defined is often the definiendum, and the expression it names being the definienlet pi: float = 3.1416 in
pi *. 2. *. 2.;;
-: float = 12.5664
<aside> 💡 the type information in the let construct is optional because OCaml is implicitly typed
</aside>
let pi = 3.1416 in
pi *. 2. *. 2. ;;
let
expressions have values# 3.1416 *. (let radius = 2.
# in radius *. radius) ;;
- : float = 12.5664
let
expressions can also be embedded in other let
expressions to get the effect of defining multiple names# let pi = 3.1416 in
# let radius = 4. in
# pi *. radius ** 2. ;;
- : float = 50.2656
let
expression is available only in the body of the expression and is therefore local to the body# (let s = "hi ho " in
# s ^ s) ^ s ;;
Line 2, characters 9-10:
2 | s ^ s) ^ s ;;
^
Error: Unbound value s
let
expression ends at the closing parenthesis and so variable s is unboundlet
naming doesn’t include the definition itself (the expre.def part between the = and the in)# let x = x + 1 in
# x * 2 ;;
Line 1, characters 8-9:
1 | let x = x + 1 in
^
Error: Unbound value x