Home / Learn / 8 Choosing

Release v0.4.0Edition 0.2

8.8 Exercises

Page 9 of 10

Each exercise starts from exercises/N/start in this chapter's download: the day-to-day program with the exercise's examples added, and a stub of what to write when there is one. una check . or una test . fails there until the exercise is done. Work in small steps: make one change, run una check ., and read what it reports before the next change.

8.8.1 Exercise 1: a lost parcel

Sometimes a carrier loses a parcel. Add a variant Lost(day: Int) to Status, describe it as "lost since day N", and refund it in full. The start package ends with these examples, and una check . reports U1001 until the variant exists:

exercises/1/start/src/tracking.unauna
example "a lost parcel says since when"
  do describe(Lost(day: 20))
  expect result == "lost since day 20"

example "a lost parcel is refunded in full"
  do refund(Lost(day: 20), paid: 4500)
  expect result == 4500

Hint: work in three steps. First add the variant at the end of Status, after a |, and run una check .: the checker shows the one when that needs a new arm. Then add that arm to describe, binding day. Last, run una test .: refund has an else, so it checks, and only the second example tells you what it answers.

What una check . reports after adding only the variant:

Diagnostic
[U2007] Error: `when` does not match variant `Lost` of `Status`
    │ Help: repair 1: add an arm for each missing variant, such as `is Lost`, or an `else` arm

With the variant and an arm in describe, una check . is clean, and una test . shows what the else of refund answers:

Output
example tracking "a lost parcel is refunded in full" ... FAILED
  src/tracking.una:176: expect result == 4500
  it is false, with `result` = `0`

9 examples: 8 passed, 1 failed

Check your answer with una test ..

Answer (check, run, test; exercises/1/answer).

Add the variant, an arm in describe, and an arm in refund:

src/tracking.unadiff
--- a/src/tracking.una
+++ b/src/tracking.una
@@ -21,3 +21,3 @@
 --- Where a shipment is. Each state carries what is known in that state, and nothing else.
-pub kind Status = Packed | InTransit(carrier: Carrier, tracking: Text) | Delivered(day: Int) | Returned(reason: ReturnReason, day: Int)
+pub kind Status = Packed | InTransit(carrier: Carrier, tracking: Text) | Delivered(day: Int) | Returned(reason: ReturnReason, day: Int) | Lost(day: Int)
 
@@ -87,2 +87,3 @@
     is Returned(reason, day) -> "returned on day {day}, {reason_text(reason)}"
+    is Lost(day) -> "lost since day {day}"
 
@@ -112,2 +113,3 @@
         paid
+    is Lost -> paid
     else -> 0

The else of refund would have answered 0 for a lost parcel. This is the cost of an else on a sum that grows: the checker cannot tell you that a new variant needs a different answer, so an example has to.

8.8.2 Exercise 2: counting the events

Write scans, which counts the events of a history. The start package has a stub that always gives 0, and these examples, the second of which fails until scans is written:

exercises/2/start/src/tracking.unauna
example "a new shipment has no events"
  do scans(Start)
  expect result == 0

example "every event counts, a repeated scan too"
  do scans(After(event: DeliveredOn(day: 3), earlier: After(event: PickedUp(carrier: Post, tracking: "PO-1"), earlier: After(event: PickedUp(carrier: Post, tracking: "PO-1"), earlier: Start))))
  expect result == 3

Hint: follow status_of. One arm per variant of History: Start has no event, and After has one event more than its earlier history. The After arm needs only the field earlier, so it can bind that one alone. A body that is a when goes on the lines under =.

Check your answer with una test ..

Answer (check, run, test; exercises/2/answer).

One arm for each variant, and a recursive call on the earlier history:

src/tracking.unadiff
--- a/src/tracking.una
+++ b/src/tracking.una
@@ -76,4 +76,8 @@
 
---- How many events a history holds. Exercise 2: count them.
-pub scans(history: History) -> Int = 0
+--- How many events a history holds.
+pub scans(history: History) -> Int
+=
+  when history
+    is Start -> 0
+    is After(earlier) -> 1 + scans(earlier)
 

Each call works on a smaller history, so the calls end at Start. The repeated pickup is counted, since scans counts events, not changes of status.

8.8.3 Exercise 3: the next action

Design a kind. For each status, the store does one thing next: wait while a parcel is packed, send the tracking number to the customer while it is in transit, ask for a review once it is delivered, put a refused parcel back on the shelf, and refund one returned for any other reason. The start package has a placeholder kind, Action, a stub for next_action, and these examples, which report U1001 until the kind has the variants they name:

exercises/3/start/src/tracking.unauna
example "a parcel in transit sends its tracking number"
  do next_action(InTransit(carrier: Post, tracking: "PO-7"))
  expect result == SendTracking(tracking: "PO-7")

example "a damaged parcel is refunded"
  do next_action(Returned(reason: Damaged, day: 4))
  expect result == Refund

example "a refused parcel goes back on the shelf"
  do next_action(Returned(reason: Refused, day: 4))
  expect result == Restock

example "a delivered parcel asks for a review"
  do next_action(Delivered(day: 3))
  expect result == AskForReview

Hint: read the examples first: they name the variants SendTracking, Refund, Restock and AskForReview, and the placeholder has Wait. Only one action carries data, so only one variant has a field. In next_action, write one arm per status; the Returned arm binds reason and chooses with an if, comparing the reason with ==, which works on a variant without fields.

Check your answer with una test ..

Answer (check, run, test; exercises/3/answer).

Declare the actions, and choose one for each status:

src/tracking.unadiff
--- a/src/tracking.una
+++ b/src/tracking.una
@@ -114,7 +114,13 @@
 
---- What the store does next about a shipment. Exercise 3: declare it as a sum kind.
-pub kind Action = | Wait
+--- What the store does next about a shipment.
+pub kind Action = Wait | SendTracking(tracking: Text) | AskForReview | Refund | Restock
 
---- The next action for a status. Exercise 3: one action for each case.
-pub next_action(status: Status) -> Action = Wait
+--- The next action for a status.
+pub next_action(status: Status) -> Action
+=
+  when status
+    is Packed -> Wait
+    is InTransit(tracking) -> SendTracking(tracking)
+    is Delivered -> AskForReview
+    is Returned(reason) -> if reason == Refused then Restock else Refund
 

is InTransit(tracking) binds one of the two fields of InTransit by its name, and SendTracking(tracking) passes it on, unlabeled since that variant has one field. The Returned arm chooses with an if, since only one reason differs; a when over the reason, with an arm for Refused and an else, would say the same.

Next:8.9 Recap and reference