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 isif true then executeThis else executeThat.Your first argument to the conditional must be anexpressionwhich returns aBoolean.This could be acode blockor some condition which you'd like to test extracted in a function.

myList = [1, 2, 3, 4, 5, 6, 7] mySimpleTerm = if 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 successiveelse ifblocks:

myList = Nat.range 0 99 myListTest = if 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

Booleanvalues can be combined with operators likeBoolean.and,also represented as&&in Unison andBoolean.or,also represented as||.

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

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")
true

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

To negate a boolean value, useBoolean.not.

Boolean.not true
false