The exercises come with the chapter's download, 09-loops.tar.gz, which holds every package of this chapter. Each exercise starts from exercises/N/start: the day-to-day program with the exercise's examples added, and a stub of the function to write, whose body gives back a placeholder. una test . fails there until the exercise is done. Run una check . as you go, and una test . until every example passes.
9.7.1 Exercise 1: the demand of the plan
Write demand, the units customers ask for over the weeks of the plan, at the product's weekly sales. The start package has the stub and two examples:
exercises/1/start/src/restock.unauna
--- The units customers ask for over `weeks` weeks, at the product's weekly sales.pubdemand(product: Product, weeks: Int) -> Units = 0
exercises/1/start/src/restock.unauna
example"the lamp is asked for 20 times in four weeks"dodemand(lamp(), weeks: 4)
expectresult == 20example"no weeks, no demand"dodemand(lamp(), weeks: 0)
expectresult == 0
Hint: an accumulator that starts at 0 and adds product.weekly_sales at each turn of 1..weeks. The result is a Units, so give the accumulator that type, or the loop forgets that it is never negative.
Check your answer with una test .. On the start package it reports:
Output
example restock "the lamp is asked for 20 times in four weeks" ... FAILED
src/restock.una:171: expect result == 20
it is false, with `result` = `0`
example restock "no weeks, no demand" ... ok
14 examples: 13 passed, 1 failed
Answer (check, run, test; exercises/1/answer).
A sum over the weeks, with a Units accumulator:
src/restock.unadiff
--- a/src/restock.una+++ b/src/restock.una@@ -85,3 +85,8 @@
--- The units customers ask for over `weeks` weeks, at the product's weekly sales.
-pub demand(product: Product, weeks: Int) -> Units = 0+pub demand(product: Product, weeks: Int) -> Units+=+ let edit total: Units = 0+ for week in 1..weeks+ total = total + product.weekly_sales+ total
The empty range of 0 weeks runs no turn, so total keeps 0, and the second example passes with no special case.
9.7.2 Exercise 2: how many orders
Write orders_placed, the number of weeks that end with an order, and add it to the last line of each product's plan, so that it reads 20 units ordered in 2 orders, 0 weeks short. The start package has the stub and two examples:
exercises/2/start/src/restock.unauna
--- How many of the `weeks` weeks end with an order.puborders_placed(product: Product, weeks: Int) -> Int = 0
exercises/2/start/src/restock.unauna
example"the lamp orders in two of four weeks"doorders_placed(lamp(), weeks: 4)
expectresult == 2example"the desk orders every week"doorders_placed(desk(), weeks: 4)
expectresult == 4
Hint: walk the weeks as units_ordered does, with the same stock accumulator, and count, in a second accumulator, the turns whose order is above 0. In print_plan, name the count with a let, as ordered and short are, and put it in the text.
Check your answer with una test ., then una run . for the new totals.
Answer (check, run, test; exercises/2/answer).
A count inside the walk of the weeks, and one more value on the last line:
src/restock.unadiff
--- a/src/restock.una+++ b/src/restock.una@@ -85,3 +85,13 @@
--- How many of the `weeks` weeks end with an order.
-pub orders_placed(product: Product, weeks: Int) -> Int = 0+pub orders_placed(product: Product, weeks: Int) -> Int+=+ let edit stock: Units = product.on_hand+ let edit orders = 0+ for week in 1..weeks+ stock = sell_week(stock, sales: product.weekly_sales)+ let order = order_for(product, stock: stock)+ if order > 0+ orders = orders + 1+ stock = stock + order+ orders@@ -108,3 +118,4 @@
let short = weeks_short(product, weeks: weeks)
- @console.print(" {ordered} units ordered, {short} weeks short")+ let orders = orders_placed(product, weeks: weeks)+ @console.print(" {ordered} units ordered in {orders} orders, {short} weeks short")
The stock must still be followed, since whether a week orders depends on what the weeks before left. The count is a plain Int, since its result needs no fact.
9.7.3 Exercise 3: the lowest stock
Write lowest_stock, the fewest units the shelf holds after a week's sales over the plan. With no weeks, it is the stock today. The stub gives back the stock today, which passes only the last of its three examples:
exercises/3/start/src/restock.unauna
--- The fewest units the shelf holds after a week's sales, over `weeks` weeks: the stock--- today when there are no weeks.publowest_stock(product: Product, weeks: Int) -> Units = product.on_hand
exercises/3/start/src/restock.unauna
example"the lamp's shelf gets down to 4 units"dolowest_stock(lamp(), weeks: 4)
expectresult == 4example"the desk's shelf runs empty"dolowest_stock(desk(), weeks: 4)
expectresult == 0example"with no weeks, the lowest stock is the stock today"dolowest_stock(rug(), weeks: 0)
expectresult == 3
Hint: two accumulators: the stock, as in stock_after, and the lowest value seen so far, which starts at the stock today. Look at the stock right after the week's sales, before the order arrives. The result is a Units, so the lowest value needs that type, and every value you assign it must be a Units too.
Check your answer with una test .. On the start package it reports:
Output
example restock "the lamp's shelf gets down to 4 units" ... FAILED
src/restock.una:172: expect result == 4
it is false, with `result` = `14`
example restock "the desk's shelf runs empty" ... FAILED
src/restock.una:176: expect result == 0
it is false, with `result` = `2`
example restock "with no weeks, the lowest stock is the stock today" ... ok
15 examples: 13 passed, 2 failed
Answer (check, run, test; exercises/3/answer).
Keep the lowest value in a Units accumulator, and lower it when the stock after the sales is below it:
src/restock.unadiff
--- a/src/restock.una+++ b/src/restock.una@@ -86,3 +86,12 @@
--- today when there are no weeks.
-pub lowest_stock(product: Product, weeks: Int) -> Units = product.on_hand+pub lowest_stock(product: Product, weeks: Int) -> Units+=+ let edit stock: Units = product.on_hand+ let edit lowest: Units = product.on_hand+ for week in 1..weeks+ stock = sell_week(stock, sales: product.weekly_sales)+ if stock < lowest+ lowest = stock+ stock = stock + order_for(product, stock: stock)+ lowest
lowest = stock proves itself, since stock is a Units. The check of the stock comes between the sales and the order, so the lamp's lowest is 4, the stock before its order arrives, and not 14.