Capstone A: the record and the total
The model
The rows of the capstone become a list of a small record, and the arithmetic lives in two functions with names: one line total and one total over a list. Money stays an integer of pence, so the total is exact.
data Row = Row
{ rowItem :: String
, rowQty :: Int
, rowPence :: Int
}
deriving (Show)
lineTotal :: Row -> Int
lineTotal row = rowQty row * rowPence row
totalPence :: [Row] -> Int
totalPence = sum . map lineTotal
lineTotal is the only place that knows a row has a
quantity and a price, and
ghc -Wall -O1 -o model model.hs
1650
sum . map lineTotal is a fold over the rows, and the
lessons on folds and on composition explain both halves. A hand-written
foldr gives the same number:
The three rows are 750, 400 and 500 pence, and 1650 is their sum. The count
is three because there are three rows, not because the total is 1650, and the
two numbers are printed side by side to make that clear.
The program
module Main (main) where
data Row = Row
{ rowItem :: String
, rowQty :: Int
, rowPence :: Int
}
deriving (Show)
rows :: [Row]
rows = [Row "tea" 3 250, Row "cake" 1 400, Row "tea" 2 250]
lineTotal :: Row -> Int
lineTotal row = rowQty row * rowPence row
totalPence :: [Row] -> Int
totalPence = sum . map lineTotal
main :: IO ()
main = do
print (totalPence rows)
print (length rows)
print (lineTotal (Row "tea" 3 250))
./model
3
750
The total is a fold
totalPence :: [Row] -> Int
totalPence = foldr (\row acc -> lineTotal row + acc) 0
What a record adds
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
lineTotal rowcompute?Answer
The quantity times the price in pence
A line costs its quantity times the unit price.
-
Why is the total an Int and not a Double?
Answer
Pence are an integer and the sum is exact
Money in the smallest unit avoids the rounding of a floating point type.
-
What does
sum . map lineTotaldo?Answer
Applies lineTotal to every row and adds the results
map produces the line totals and sum adds them.
-
What does this program print?
module Main (main) where main :: IO () main = print (sum (map (* 2) [1, 2, 3 :: Int]))Answer
12
The doubled values are 2, 4 and 6, and their sum is 12.
-
What is the line total of
Row "tea" 2 250?Answer
500
Two units at 250 pence are 500 pence.
-
What does this program print?
module Main (main) where main :: IO () main = print (length [1 .. 4 :: Int])Answer
4
The range has four elements.
-
Why is the arithmetic in two named functions?
Answer
One change to the rule is one change in one place
Names give the rule a single definition that the report and the tests share.
-
What does this program print?
module Main (main) where main :: IO () main = print (foldr (+) 0 [1, 2, 3 :: Int])Answer
6
The fold adds the three numbers from a zero start.
-
What does the count of the rows tell you?
Answer
How many lines the file had, not the total
The count and the total are different facts about the same list.
-
What does this program print?
module Main (main) where main :: IO () main = print (3 * 250 :: Int)Answer
750
Three units at 250 pence are 750 pence.
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