The exercises come with the chapter's download, 02-numbers.tar.gz. Each starts from exercises/N/start: the day-to-day program with the exercise's examples added at the end, and a stub of each function to write: its header, with a body that gives back a placeholder, a value of the right type that is not the answer, such as the empty text "" or 0. una test . fails there until the exercise is done. Work in this order:
Read the examples: they say what each function must give back.
Write the function's body, in place of the placeholder.
Run una check ., and fix what it reports.
Run una test . until every example passes, then una run . to see the slip.
2.7.1 Exercise 1: the weight of the parcel
The warehouse wants the weight on the slip, in kilograms and grams. Write weight_line(grams), which turns 2350 into Parcel weight: 2 kg 350 g. The start package ends with:
exercises/1/start/src/slip.unauna
--- The weight of a parcel, in kilograms and grams: 2350 is "Parcel weight: 2 kg 350 g".pubweight_line(grams: Int) -> Text = ""example"a weight shows whole kilograms and the grams left over"doweight_line(2350)
expectresult == "Parcel weight: 2 kg 350 g"example"a parcel under one kilogram shows 0 kg"doweight_line(800)
expectresult == "Parcel weight: 0 kg 800 g"
Hint: a kilogram is 1000 grams. / divides two Int values and drops the remainder, and mod gives the remainder: 2350 / 1000 is 2, and 2350 mod 1000 is 350. Both can go inside the braces of a text, as in Putting values into a text.
Check your answer with una test .. On the start package it reports:
Output
example slip "a weight shows whole kilograms and the grams left over" ... FAILED
src/slip.una:102: expect result == "Parcel weight: 2 kg 350 g"
it is false, with `result` = `""`
example slip "a parcel under one kilogram shows 0 kg" ... FAILED
src/slip.una:106: expect result == "Parcel weight: 0 kg 800 g"
it is false, with `result` = `""`
13 examples: 11 passed, 2 failed
The placeholder gives back the empty text, "", which is what each report shows as result.
Answer (check, run, test; exercises/1/answer).
Give weight_line its body:
src/slip.unadiff
--- a/src/slip.una+++ b/src/slip.una@@ -97,3 +97,3 @@
--- The weight of a parcel, in kilograms and grams: 2350 is "Parcel weight: 2 kg 350 g".
-pub weight_line(grams: Int) -> Text = ""+pub weight_line(grams: Int) -> Text = "Parcel weight: {grams / 1000} kg {grams mod 1000} g"
Each brace holds one computation, and the text puts the results in their places.
2.7.2 Exercise 2: shipping and the amount to pay
The slip should also show the shipping charge, 450 cents for this order, and what the customer pays in all. Write to_pay(goods, shipping) and pay_line(cents), and print two new lines after the total: Shipping: 450 cents and To pay: 8930 cents. The start package ends with:
exercises/2/start/src/slip.unauna
--- What the customer pays: the goods and the shipping, in cents.pubto_pay(goods: Int, shipping: Int) -> Int = 0--- The line with the amount to pay.pubpay_line(cents: Int) -> Text = ""example"shipping is added to the goods"doto_pay(8480, shipping: 450)
expectresult == 8930example"the pay line is in cents"dopay_line(8930)
expectresult == "To pay: 8930 cents"
Hint:to_pay adds its two parameters, and pay_line is written as total_line is. In main, give the sum of the two lines a name with let, so that both total_line and to_pay can use it, and print the new lines after the total.
Check your answer with una test ., then una run . for the two new lines.
Answer (check, run, test; exercises/2/answer).
Give both functions their bodies, and print the new lines in main:
src/slip.unadiff
--- a/src/slip.una+++ b/src/slip.una@@ -49,3 +49,6 @@
@console.print(box_line(boxes(units)))
- @console.print(total_line(lamp_cents + rug_cents))+ let goods = lamp_cents + rug_cents+ @console.print(total_line(goods))+ @console.print("Shipping: 450 cents")+ @console.print(pay_line(to_pay(goods, shipping: 450)))
@console.print(closing())
@@ -97,6 +100,6 @@
--- What the customer pays: the goods and the shipping, in cents.
-pub to_pay(goods: Int, shipping: Int) -> Int = 0+pub to_pay(goods: Int, shipping: Int) -> Int = goods + shipping
--- The line with the amount to pay.
-pub pay_line(cents: Int) -> Text = ""+pub pay_line(cents: Int) -> Text = "To pay: {cents} cents"
goods names the sum once, and two lines use it. to_pay(goods, shipping: 450) labels its second argument, as every argument after the first is labeled.
2.7.3 Exercise 3: amounts with two decimals
Customers read 84.80 more easily than 8480 cents. Write amount(cents), which writes an amount of cents with two decimals, and use it in total_line, so that the slip says Total paid: 84.80. Watch the cents under ten: 1205 is 12.05, not 12.5. The start package ends with:
exercises/3/start/src/slip.unauna
--- An amount of cents with two decimals: 8480 is "84.80", and 1205 is "12.05".pubamount(cents: Int) -> Text = ""example"an amount has two decimals"doamount(8480)
expectresult == "84.80"example"cents under ten keep their zero"doamount(1205)
expectresult == "12.05"example"less than one unit starts with 0"doamount(7)
expectresult == "0.07"
Hint: write the whole units, a point, and then the two digits of the cents one by one: the tens of cents and the cents left over. Each is a / or a mod of cents, or both. For 1205: the units are 1205 / 100, 12; the cents are 1205 mod 100, 5; the tens of cents are 5 / 10, 0; and the last digit is 5 mod 10, 5.
Check your answer with una test ., then una run . for the new total. The example of total_line must change too, since the line it tests changes.
Answer (check, run, test; exercises/3/answer).
Give amount its body, and use it in total_line and its example:
src/slip.unadiff
--- a/src/slip.una+++ b/src/slip.una@@ -28,3 +28,3 @@
--- What the customer paid for the goods, in cents.
-pub total_line(cents: Int) -> Text = "Total paid: {cents} cents"+pub total_line(cents: Int) -> Text = "Total paid: {amount(cents)}"@@ -92,8 +92,8 @@-example "the total is in cents"+example "the total is an amount"
do total_line(8480)
- expect result == "Total paid: 8480 cents"+ expect result == "Total paid: 84.80"
--- An amount of cents with two decimals: 8480 is "84.80", and 1205 is "12.05".
-pub amount(cents: Int) -> Text = ""+pub amount(cents: Int) -> Text = "{cents / 100}.{cents mod 100 / 10}{cents mod 10}"
cents mod 100 / 10 is the tens of cents, and cents mod 10 the cents left over, so each digit is printed on its own and a zero is never lost. This works for an amount that is not negative; a refund would need the rule of Euclidean division, and a choice that section 3.4 teaches.