Home / Learn

Release v0.4.0

9 Repeating: for

Page 1 of 9

Until now, a step that runs four times was written four times, or put in a function that calls itself. A plan for four weeks had four lines, and a plan for five weeks needed a fifth. Real work repeats: the same check for every week of a plan, every character of a SKU, every aisle of a store. This chapter teaches the for loop, which runs the same steps once for each number of a range or each character of a text, and let edit, the name that a loop changes from one turn to the next. It ends with what the verifier knows about a loop, and a restock plan built from both.

What you'll build

A restock plan for the next four weeks. For each product, it follows the stock week by week: what is left after the week's sales, and the order the store places when the stock falls below its reorder point. It ends each product with the units ordered and the weeks the shelf started too low. The program prints:

Output
Restock plan, 4 weeks
LAMP-01, LAMP
  week 1: 9 left
  week 2: 4 left, order 10
  week 3: 9 left
  week 4: 4 left, order 10
  20 units ordered, 0 weeks short
RUG-07, RUG
  week 1: 1 left, order 4
  week 2: 3 left
  week 3: 1 left, order 4
  week 4: 3 left
  8 units ordered, 0 weeks short
DESK-02, DESK
  week 1: 0 left, order 2
  week 2: 0 left, order 2
  week 3: 0 left, order 2
  week 4: 0 left, order 2
  8 units ordered, 4 weeks short

You will learn to:

  • repeat steps with for, over a range of whole numbers or the characters of a text;
  • choose between .., which includes the end of a range, and ..<, which leaves it out;
  • keep a total, a count or a growing text in an editable name, let edit;
  • give an editable name a refined type, so that the verifier keeps its fact through the loop;
  • read U2019, U5002 and the other diagnostics a loop can give.

Download this chapter's packages: each step is a package under steps/, and the chapter's program is day-to-day/; run una check . and una run . in one.

Why it works this way

In many languages, a loop is where a program goes wrong most quietly. A loop that counts from 0 to 10 when it meant 0 to 9 reads one item past the end. A loop that repeats "while the stock is above zero" never ends when its body forgets to lower the stock, and the program hangs. A name that any line may change makes a reader trace every line to know what it holds.

Una keeps the loop small and visible. A for loop walks a range of numbers, or a list such as the characters of a text, and the number of turns is fixed before the first turn: a reader sees where the loop ends from its first line. The range says in its own syntax whether its end is included. A name changes only when it is declared let edit, so a plain let still holds the value it was given. And the verifier checks a loop's body once, for every turn at the same time: what it keeps from one turn to the next is what the types of the changing names say, and nothing else.

Key idea

In Una, a for loop takes one turn for each value of a range or a list fixed before it starts, and only a name declared let edit changes from turn to turn.

View the Markdown

Next:9.1 for over ranges and texts