Home / Learn / 6 Functions and their results

Release v0.4.0Edition 0.2

6.7 Exercises

Page 8 of 9

The exercises come with the chapter's download, 06-functions.tar.gz. Each one starts from exercises/N/start: the day-to-day program with the exercise's example added at the end, and a stub of the function to write: its header, with a body that gives back a placeholder, 0. 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. Write the function, in whatever layout comes to you.
  3. Run una fmt ., which puts it in canonical form, then una check . until it is clean.
  4. Run una test . until every example passes.

6.7.1 Exercise 1: delivery days

Write delivery_days(zone): 2 days within the country, 4 to a neighboring country, and 9 to the rest of the world. The start package ends with:

exercises/1/start/src/shipping.unauna
--- The days an order takes to arrive: 2 within the country, 4 to a neighboring country, and 9
--- to the rest of the world.
pub delivery_days(zone: Zone) -> Int = 0

example "an order within the country arrives in two days"
  do delivery_days(Home)
  expect result == 2

example "an order to the rest of the world takes nine days"
  do delivery_days(World)
  expect result == 9

Hint: surcharge has the same shape: one value per zone. Its body is a when with one arm per zone, and a when of several lines takes the long form.

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

Output
example shipping "an order within the country arrives in two days" ... FAILED
  src/shipping.una:130: expect result == 2
  it is false, with `result` = `0`
example shipping "an order to the rest of the world takes nine days" ... FAILED
  src/shipping.una:134: expect result == 9
  it is false, with `result` = `0`

11 examples: 9 passed, 2 failed
Answer (check, run, test; exercises/1/answer).

Give delivery_days a when with one arm per zone:

src/shipping.unadiff
--- a/src/shipping.una
+++ b/src/shipping.una
@@ -125,3 +125,8 @@
 --- to the rest of the world.
-pub delivery_days(zone: Zone) -> Int = 0
+pub delivery_days(zone: Zone) -> Int
+=
+  when zone
+    is Home -> 2
+    is Neighbor -> 4
+    is World -> 9
 

The result type is Int, so each arm gives an Int, and the when, the function's last line, is its result.

6.7.2 Exercise 2: a members' price

Members pay 20 percent less for shipping, rounded down to a whole cent. Write members_price(charge), with a header that promises, like weekend_price, that the price is never negative and never more than the charge. Then print order 1047 in main: 2500 cents of goods, 800 g, to a neighboring country, at the members' price. The start package ends with:

exercises/2/start/src/shipping.unauna
--- The members' price: 20 percent off the charge, rounded down. It never raises the charge.
pub members_price(charge: Int) -> Int = 0

example "members pay 20 percent less"
  do members_price(1_390)
  expect result == 1_112

Hint: 20 percent off is 80 percent of the charge: charge * 80 / 100. Copy the clauses of weekend_price. In main, build the order with let, as for mugs, and pass members_price(member.quote) as the charge.

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

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

Give members_price its clauses and its body, and print the new order:

src/shipping.unadiff
--- a/src/shipping.una
+++ b/src/shipping.una
@@ -86,2 +86,4 @@
   show(mugs, charge: mugs.quote)
+  let member = Order(number: 1047, goods: 2_500, parcel: Parcel(grams: 800, zone: Neighbor))
+  show(member, charge: members_price(member.quote))
 
@@ -124,3 +126,7 @@
 --- The members' price: 20 percent off the charge, rounded down. It never raises the charge.
-pub members_price(charge: Int) -> Int = 0
+pub members_price(charge: Int) -> Int
+  requires charge >= 0
+  ensures result >= 0 and result <= charge
+=
+  charge * 80 / 100
 

The clauses take the long form. The verifier proves both promises for every charge that is not negative, and the call in main checks because quote promises a charge that is not negative.

6.7.3 Exercise 3: two orders packed together

Two orders to the same customer are packed together, and pay the charge of each. Write pair_quote(first, second), which gives back the sum of the two quotes, with a header that promises a result that is not negative. The start package ends with:

exercises/3/start/src/shipping.unauna
--- What shipping costs for two orders packed together, in cents: the charge of each.
pub pair_quote(first: Order, second: Order) -> Int = 0

example "two orders pay the charge of each"
  do pair_quote(Order(number: 1, goods: 3_980, parcel: Parcel(grams: 1_200, zone: Home)), second: Order(number: 2, goods: 2_500, parcel: Parcel(grams: 800, zone: Neighbor)))
  expect result == 2_180

Hint: the body is one line, with two method calls: first.quote + second.quote. Each call of quote has a precondition, so pair_quote must require it for both orders. The verifier then proves your ensures from quote's own.

Check your answer with una test ..

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

Require a weight for each parcel, and promise a sum that is not negative:

src/shipping.unadiff
--- a/src/shipping.una
+++ b/src/shipping.una
@@ -124,3 +124,8 @@
 --- What shipping costs for two orders packed together, in cents: the charge of each.
-pub pair_quote(first: Order, second: Order) -> Int = 0
+pub pair_quote(first: Order, second: Order) -> Int
+  requires first.parcel.grams > 0
+  requires second.parcel.grams > 0
+  ensures result >= 0
+=
+  first.quote + second.quote
 

Without the two requires lines, the calls of quote do not check: nothing tells the verifier that each parcel weighs something. Without the ensures, the function checks, but a caller of pair_quote would know nothing about its result.

Next:6.8 Recap and reference