Controlling program execution

This section details a few of the fundamental language constructs for directing the order in which a program executes.

We'll refer to this as "control flow" from now on and we'll cover how to write if/else statements, how to pattern match, how to deal with the concept of looping or iterating through collections, and some error handling basics.

If, then, else

Unison's syntax for conditional logic is if true then executeThis else executeThat. Your first argument to the conditional must be an expression which returns a Boolean. This could be a code block or some condition which you'd like to test extracted in a function.

myList = [1, 2, 3, 4, 5, 6, 7] mySimpleTerm = if distributed.lib.base.Search.elem 5 myList then "high five" else "no five found" mySimpleTerm
"high five"

If we wanted many if/else statements expressing multiple conditions, we'd express it with successive else if blocks:

myList = Nat.range 0 99 myListTest = if distributed.lib.base.Search.elem 100 myList then "100 found" else if List.any (elem -> Nat.mod elem 2 === 0) myList then "Even found" else "Condition not met" myListTest
"Even found"

Boolean Expressions

Boolean values can be combined with operators like Boolean.and, also represented as && in Unison and Boolean.or, also represented as ||.

false && (base.bug "oh no")

For an expression where the first argument to && is false, as in the one above, the latter argument will never be evaluated.

true || (base.bug "oh no")

For an expression where the first argument to || is true, the latter argument will never be evaluated.

To negate a boolean value, use Boolean.not.

Boolean.not true