Home / Learn / 4 Kinds

Release v0.4.0Edition 0.2

4.8 Exercises

Page 9 of 10

The exercises come with the chapter's download, 04-kinds.tar.gz. Each starts from exercises/N/start: the day-to-day program with the exercise's example added. Work in this order: read the example, change the code, run una check . and fix what it reports, then run una test . until every example passes.

4.8.1 Exercise 1: lockers cost 100 cents

The locker company now charges the store, and the store passes it on: a locker delivery costs 100 cents. The example for lockers has already been changed:

exercises/1/start/src/pricing.unauna
example "a locker costs 100 cents, and is named"
  do delivery_charge(AtLocker("Station 14"))
  expect result == Charge(item: "locker Station 14", cents: 100)

Change delivery_charge so that the example passes.

Hint: only one arm of the when in delivery_charge changes. Its doc comment, which says that only the door costs extra, should change too.

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

Output
example pricing "a locker costs 100 cents, and is named" ... FAILED
  src/pricing.una:144: expect result == Charge(item: "locker Station 14", cents: 100)
  it is false, with `result` = `Charge(item: "locker Station 14", cents: 0)`
15 examples: 14 passed, 1 failed
Answer (check, run, test; exercises/1/answer).

Give the locker arm its new amount:

src/pricing.unadiff
--- a/src/pricing.una
+++ b/src/pricing.una
@@ -51,3 +51,3 @@
 
---- The charge for the last step: only the door costs extra.
+--- The charge for the last step: the door costs the most, and a locker a little.
 delivery_charge(delivery: Delivery) -> Charge[Text]
@@ -56,3 +56,3 @@
     is AtDoor -> Charge(item: "to the door", cents: 250)
-    is AtLocker(locker) -> Charge(item: "locker {locker}", cents: 0)
+    is AtLocker(locker) -> Charge(item: "locker {locker}", cents: 100)
     is AtStore(store) -> Charge(item: "store {store}", cents: 0)

The first parcel goes to a locker, so its total is now 700 cents.

4.8.2 Exercise 2: overnight shipping

The store adds a third speed, overnight, at 1500 cents in every zone. The start package adds the variant and its example:

exercises/2/start/src/pricing.unauna
--- How fast a parcel travels.
kind Speed = Standard | Express | Overnight
exercises/2/start/src/pricing.unauna
example "overnight costs 1500 in every zone"
  do speed_charge(Overnight, zone: Local)
  expect result == Charge(item: Overnight, cents: 1500)

una check . already says where the program needs to change:

Diagnostic
[U2007] Error: `when` does not match variant `Overnight` of `Speed`
    ╭─[ src/pricing.una:48:3 ]
    │
 48 │   when speed
    │   ──┬─
    │     ╰─── `when` does not match variant `Overnight` of `Speed`
    │
    │ Help: repair 1: add an arm for each missing variant, such as `is Overnight`, or an `else` arm
    │
    │ Note: run `una explain U2007` for an explanation

Make the program check, and the example pass.

Hint: the message names the when and the missing variant. Add an arm for it. Do not add an else: a fourth speed should be reported in the same way.

Check your answer with una test ..

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

Add the arm for Overnight to speed_charge:

src/pricing.unadiff
--- a/src/pricing.una
+++ b/src/pricing.una
@@ -50,2 +50,3 @@
     is Express -> Charge(item: speed, cents: if zone is Abroad then 900 else 500)
+    is Overnight -> Charge(item: speed, cents: 1500)
 

No other function chooses by the speed, so this is the only change. The checker found it for you.

4.8.3 Exercise 3: the cheaper of two charges

A clerk wants to tell customers which of two ways to collect a parcel costs less. Write cheaper, which gives back the charge with fewer cents, and the first one when the two cost the same. It must work for a Charge of any type. The start package ends with:

exercises/3/start/src/pricing.unauna
--- The cheaper of two charges, and the first one when they cost the same.
cheaper[T](first: Charge[T], second: Charge[T]) -> Charge[T] = first

example "a locker is cheaper than the door"
  do cheaper(delivery_charge(AtDoor), second: delivery_charge(AtLocker("Station 14")))
  expect result == Charge(item: "locker Station 14", cents: 0)

example "on a tie, the first charge"
  do cheaper(delivery_charge(AtLocker("Station 14")), second: delivery_charge(AtStore("Harbour Road")))
  expect result == Charge(item: "locker Station 14", cents: 0)

Hint: compare first.cents and second.cents with an if. Ask yourself whether the body does anything with a value of T: it compares the cents fields, which are Int, and only moves the charges. So T needs no constraint.

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

Output
example pricing "a locker is cheaper than the door" ... FAILED
  src/pricing.una:163: expect result == Charge(item: "locker Station 14", cents: 0)
  it is false, with `result` = `Charge(item: "to the door", cents: 250)`
17 examples: 16 passed, 1 failed
Answer (check, run, test; exercises/3/answer).

Give back the second charge only when it costs less:

src/pricing.unadiff
--- a/src/pricing.una
+++ b/src/pricing.una
@@ -158,3 +158,3 @@
 --- The cheaper of two charges, and the first one when they cost the same.
-cheaper[T](first: Charge[T], second: Charge[T]) -> Charge[T] = first
+cheaper[T](first: Charge[T], second: Charge[T]) -> Charge[T] = if second.cents < first.cents then second else first
 

second.cents < first.cents, and not <=, keeps the first charge on a tie. The function never compares two values of T, so [T] with no constraint is enough, and cheaper works on a Charge[Zone] as well as on a Charge[Text].

Next:4.9 Recap and reference