Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Boolean Operators

import Lait
#lait

The Bool type is defined by the values true and false.

Logical Operators

We use the infix operators &&, ||, and the prefix operator not to build expressions of type Bool:

#eval true || false>> true
#eval false && true>> false
#eval not false>> true
#eval not (true && not false)>> false

Comparison Operators

We can also create expressions of type Bool by using equality, ==:

#eval 1 == 2>> false
#eval false == false>> true
#eval "hello" == "hello">> true

Given two expressions e1 and e2 of the same type, we can form e1 == e2, which has type Bool. (Note that equality doesn’t work for all types; e.g., function types, which we will see later.) We also have the shorthand e1 != e2 for not (e1 == e2):

#eval 1 != 2>> true
#eval false != false>> false
#eval "hello" != "hello">> false

The ordinary comparison operators on Int are <, >, <=, and >=:

#eval 1 < 2>> true
#eval 3 >= 3>> true
#eval 4 <= 2>> false
#eval 7 > (-2)>> true

If/then/else

To use a Bool, the primary way is to use it in an if/then/else expression:

#eval if false then 1 else 2>> 2

Given a boolean expression e, and two other expressions e1/e2 of the same type T, if e then e1 else e2 has type T.