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:
Read the example: it says what the function must give back.
Change the code.
Run una check ., and fix what it reports.
Run una test . until every example passes, then una run . to see the notices.
1.9.1 Exercise 1: the help desk moves
The help desk moves next to the café. The example of help_desk has already been changed to the new text:
exercises/1/start/src/morning.unauna
example"the help desk says where to go"dohelp_desk()
expectresult == "Questions? Ask at the help desk by the café"
Change help_desk so that the example passes.
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:
Output
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 (check, run, test; exercises/1/answer).
Give help_desk the new text:
src/morning.unadiff
--- 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
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:
exercises/2/start/src/morning.unauna
--- How long customers have to bring an item back.pubreturns_policy() -> Text = ""example"returns are free for 30 days"doreturns_policy()
expectresult == "Returns are free within 30 days"
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 (check, run, test; exercises/2/answer).
Give returns_policy its text, and print it in main:
src/morning.unadiff
--- 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
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:
exercises/3/start/src/morning.unauna
--- When the store is open on Sundays: the same hours as every other day.pubsunday_hours() -> Text = ""example"Sundays keep the hours of every other day"dosunday_hours()
expectresult == opening_hours()
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 (check, run, test; exercises/3/answer).
Make the body of sunday_hours a call of opening_hours, and print it in main:
src/morning.unadiff
--- 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.