Capstone A: the report
The report
A report groups the rows by item, totals each group, sorts the groups by name
and prints them as a table. Three library functions do the work:
sortOn fst for the order and
totals :: Map.Map String Int
totals = Map.fromListWith (+) [(rowItem r, lineTotal r) | r <- rows]
Map is already ordered by key, and
ghc -Wall -O1 -o report report.hs
item pence
The widths in the format string are the whole design of the table: the name
column is eight characters and left-aligned, the pence column is eight and
right-aligned, and the header uses the same two. The rows line up because
every line is formatted with the same widths, not because the data is short.
The total is printed with the same two widths, so it sits under the column
it belongs to.
The numbers are checkable by hand: tea is three units at 250 plus two at 250,
so 1250; cake is one at 400; the total is 1650. A report that prints a total
different from the sum of its visible rows is the first bug a reader finds,
which is why the total comes from the same map and not from a second pass
over the rows.
The next lesson reads the file lazily, so the report of a file that does not
fit in memory is still one pass. A new set of questions in a new order every time. Choose one answer for each, then check. What does Combines their values with + The combining function is applied to the old and the new value. Why is It makes the order explicit for the reader The map is ordered, and the sort states the order the report wants. What does Right-aligns an integer in eight characters The width is a minimum and the default alignment is right. What does this program print?
[("a",3)] The duplicate key is combined by + and the map has one entry. What is the group total for tea with rows 3 x 250 and 2 x 250? 1250 pence Three at 250 plus two at 250 is 1250. What does this program print?
1650 The two group totals add to 1650. Why does the total come from the map and not a second pass? So the printed total cannot disagree with the printed rows One source for the numbers keeps the table consistent. What does this program print?
[(1,"a"),(2,"b")] sortOn fst orders the pairs by their first component. Which function formats a fixed-width row? printf takes the widths and the values and returns the formatted line. What does Left-aligns a string in eight characters The minus sign asks for left alignment and 8 is the minimum width.
The code on this page was built with GHC 9.10.3 and run on
Alpine Linux 3.24.3 (x86_64) in a
Docker container.
Article author: Arthur Isaev
The program
module Main (main) where
import Data.List (sortOn)
import qualified Data.Map.Strict as Map
import Text.Printf (printf)
data Row = Row
{ rowItem :: String
, rowQty :: Int
, rowPence :: Int
}
rows :: [Row]
rows = [Row "tea" 3 250, Row "cake" 1 400, Row "tea" 2 250]
totals :: Map.Map String Int
totals =
Map.fromListWith (+) [(rowItem r, rowQty r * rowPence r) | r <- rows]
main :: IO ()
main = do
printf "%-8s %8s\n" "item" "pence"
mapM_
(uncurry (printf "%-8s %8d\n"))
(sortOn fst (Map.toList totals))
printf "%-8s %8d\n" "total" (sum (Map.elems totals))
./report
cake 400
tea 1250
total 1650
The table and its columns
What the report checks
Questions
Map.fromListWith (+) do with duplicate keys?Answer
sortOn fst used when a Map is already ordered?Answer
%8d do?Answer
module Main (main) where
import qualified Data.Map.Strict as Map
main :: IO ()
main = print (Map.toList (Map.fromListWith (+) [("a", 1), ("a", 2)]))Answer
Answer
module Main (main) where
main :: IO ()
main = print (sum [400, 1250 :: Int])Answer
Answer
module Main (main) where
import Data.List (sortOn)
main :: IO ()
main = print (sortOn fst [(2, "b"), (1, "a")])Answer
Answer
Text.Printf.printf%-8s do in the format string?Answer