---
title: "1.9 Exercises"
url: https://una-lang.org/en/learn/hello/exercises/
release: v0.4.0
edition: "0.2"
---

# 1.9 Exercises

The exercises come with the chapter's download, `01-hello.tar.gz`, which holds every package of this chapter, each ready to check and run. Each exercise starts from `exercises/N/start`: the day-to-day program with the exercise's example added, and, when the exercise asks for a new function, a **stub** of it: its header with a body that gives back a **placeholder**, the empty text `""`. `una test .` fails there until the exercise is done. Work in this order:

1. Read the example: it says what the function must give back.
2. Change the code.
3. Run `una check .`, and fix what it reports.
4. Run `una test .` until every example passes, then `una run .` to see the notices.

## 1.9.1 Exercise 1: the help desk moves

::::exercise
The help desk moves next to the café. The example of `help_desk` has already been changed to the new text:

```una include="exercises/1/start/src/morning.una#L85-L87" title="exercises/1/start/src/morning.una"
example "the help desk says where to go"
  do help_desk()
  expect result == "Questions? Ask at the help desk by the café"
```

Change `help_desk` so that the example passes.

:::hint
*Hint:* only the text in the body of `help_desk` changes. Copy the new text from the example, so that not one character differs.
:::

Check your answer with `una test .`. On the start package it reports:

```text include="exercises/1/start/expected/commands/test.txt#L9-L13"
example morning "the help desk says where to go" ... FAILED
  src/morning.una:87: expect result == "Questions? Ask at the help desk by the café"
  it is false, with `result` = `"Questions? Ask at the help desk by the main door"`

8 examples: 7 passed, 1 failed
```

:::answer Answer (*check, run, test*; `exercises/1/answer`).
Give `help_desk` the new text:

```diff include="exercises/1/start..exercises/1/answer:src/morning.una" context="1" title="src/morning.una"
--- a/src/morning.una
+++ b/src/morning.una
@@ -26,3 +26,3 @@
 --- Where customers can ask a question.
-pub help_desk() -> Text = "Questions? Ask at the help desk by the main door"
+pub help_desk() -> Text = "Questions? Ask at the help desk by the café"
 
```

The example states the new text, the function gives it back, and `main` prints it without a change of its own.
:::
::::

## 1.9.2 Exercise 2: a notice for returns

::::exercise
Add a part for returns. Write `returns_policy()`, which gives back `Returns are free within 30 days`, and print it in `main` under the heading `Returns`, after the café. The start package ends with:

```una include="exercises/2/start/src/morning.una#L89-L94" title="exercises/2/start/src/morning.una"
--- How long customers have to bring an item back.
pub returns_policy() -> Text = ""

example "returns are free for 30 days"
  do returns_policy()
  expect result == "Returns are free within 30 days"
```

:::hint
*Hint:* the body of `returns_policy` is its text, as for the other notices. In `main`, add one call of `section`, written as the one for the café is.
:::

Check your answer with `una test .`, then `una run .` for the new part.

:::answer Answer (*check, run, test*; `exercises/2/answer`).
Give `returns_policy` its text, and print it in `main`:

```diff include="exercises/2/start..exercises/2/answer:src/morning.una" context="1" title="src/morning.una"
--- a/src/morning.una
+++ b/src/morning.una
@@ -54,2 +54,3 @@
   section("Café", notice: cafe_hours())
+  section("Returns", notice: returns_policy())
   @console.print(help_desk())
@@ -89,3 +90,3 @@
 --- How long customers have to bring an item back.
-pub returns_policy() -> Text = ""
+pub returns_policy() -> Text = "Returns are free within 30 days"
 
```

The new part prints its heading, its notice and a rule, like every other part, since `section` does the printing.
:::
::::

## 1.9.3 Exercise 3: Sunday hours that follow the week

::::exercise
On Sundays the store keeps the hours of every other day. Write `sunday_hours()` so that it always gives back what `opening_hours()` gives back, even after the hours change, and print it in `main` under the heading `Sundays`, after the hours. The start package ends with:

```una include="exercises/3/start/src/morning.una#L89-L94" title="exercises/3/start/src/morning.una"
--- When the store is open on Sundays: the same hours as every other day.
pub sunday_hours() -> Text = ""

example "Sundays keep the hours of every other day"
  do sunday_hours()
  expect result == opening_hours()
```

:::hint
*Hint:* the example compares with `opening_hours()`, not with a text. Copying the text into `sunday_hours` passes it today, but not after the hours change. A call stands for the value it gives back, so a function's body may be a call of another function that gives back the same type.
:::

Check your answer with `una test .`, then `una run .`.

:::answer Answer (*check, run, test*; `exercises/3/answer`).
Make the body of `sunday_hours` a call of `opening_hours`, and print it in `main`:

```diff include="exercises/3/start..exercises/3/answer:src/morning.una" context="1" title="src/morning.una"
--- a/src/morning.una
+++ b/src/morning.una
@@ -51,2 +51,3 @@
   section("Hours", notice: opening_hours())
+  section("Sundays", notice: sunday_hours())
   section("Shipping", notice: shipping_cutoff())
@@ -89,3 +90,3 @@
 --- When the store is open on Sundays: the same hours as every other day.
-pub sunday_hours() -> Text = ""
+pub sunday_hours() -> Text = opening_hours()
 
```

The hours are still written once, in `opening_hours`. When they change, the Sunday line changes with them.
:::
::::
