Ch.6 Introduction to ML programming

§ 6.5. Type bool and conditional expressions

Following the ML principle,

programming is done by defining an expression that represents the desired value.

conditional computation is represented as an expression. A conditional expression

if E1 then E2 else E3

is an expression that computes a value in the following steps.

  1. Evaluate E1 and obtain a value.

  2. If the value is true then evaluate E2 and return its value.

  3. If the value of E1 is false then evaluate E3 return its value.

true, false are two constants of type bool. Expressions of type bool contains comparison expressions and logical operations.

# 1 < 2;
val it = true : bool
# 1 < 2 andalso 1 > 2;
val it = false : bool
# 1 < 2 orelse 1 > 2;
val it = true : bool

Conditional expressions are made up with expressions of type bool.

# if 1 < 2 then 1 else 2;
val it = 1 : int

Since this is an expression, it can be combined with other expressions as show below.

# (if 1 < 2 then 1 else 2) * 10;
val it = 10 : int