Control flow
prepoly has the usual control-flow constructs: if/else, while, for,
break, and continue. A distinctive point is that if (and match, covered
in Pattern matching) are expressions — they yield a
value.
if is an expression
Section titled “if is an expression”fun grade(score) { let result = if score >= 60 { "pass" } else { "fail" } return result}
println(grade(72)) // passprintln(grade(31)) // failelse if chains work as you would expect:
fun size_of(n) { if n < 10 { return "small" } else if n < 100 { return "medium" } else { return "large" }}Here is the Collatz step counter — while runs as long as the condition holds:
fun collatz_steps(n) { let count = 0 let x = n while x != 1 { if x % 2 == 0 { x = x / 2 } else { x = 3 * x + 1 } count += 1 } return count}
for n in [6, 7, 27] { println("collatz({n}) = {collatz_steps(n)} steps")}for over arrays and ranges
Section titled “for over arrays and ranges”for x in xs iterates the elements of an array. The bracket form [lo..hi]
builds the half-open integer range lo, lo+1, ..., hi-1, so counting loops
look like this:
let sum = 0for i in [1..11] { sum += i}println(sum) // 55break and continue
Section titled “break and continue”continue skips to the next iteration, break exits the loop:
let sum = 0for n in [1, 2, 3, 4, 5, 6, 7, 8] { if n % 2 == 1 { continue } if n > 6 { break } sum += n}println("sum of evens up to 6 = {sum}") // 12There is no statement terminator: a newline ends a statement. A line continues
onto the next when it ends with a binary operator or when the next line starts
with . (a method chain) — see Syntax for the exact
rules.