Capstone A: reading the file
A capstone in seven lessons
The first capstone reads a file of sales rows, parses them, totals them by item and prints a report that a user can read. Every lesson adds one piece, and every lesson shows a complete program: the pieces are the same code in a larger program, not a diff.
The input is a CSV file with a header and four columns: the date, the item, the quantity and the price in pence. Money is an integer of pence for the reason the numbers lesson gave: a Double cannot hold 2.50 exactly, and a total of a few thousand rows shows it.
date,item,qty,pence
2026-11-01,tea,3,250
2026-11-02,cake,1,400
2026-11-02,tea,2,250
Reading the file
The program writes the file it is about to read, so the page needs no separate data file and the run is the same everywhere. The reading is readFile and lines, which the files lesson used: a String in, a list of lines out.
module Main (main) where
csv :: String
csv =
unlines
[ "date,item,qty,pence"
, "2026-11-01,tea,3,250"
, "2026-11-02,cake,1,400"
, "2026-11-02,tea,2,250"
]
main :: IO ()
main = do
writeFile "/tmp/sales.csv" csv
text <- readFile "/tmp/sales.csv"
let rows = lines text
print (length rows)
mapM_ putStrLn (take 2 rows)
ghc -Wall -O1 -o read read.hs
./read
4
date,item,qty,pence
2026-11-01,tea,3,250
The shape of the data
Four lines: one header and three rows. The header is data like any other line, and dropping it is a decision the parser makes in the next lesson, not something the file does for free. The three rows are the whole input of the capstone, and the numbers in them are the ones the report must reproduce.
- The first line is the header and is skipped by the parser.
- Each row has four fields and no quoted commas, which is why splitOn "," is enough for this file.
- The last field is pence and not pounds, so 250 is 2.50.
What the rest of the capstone adds
Six lessons remain: a parser with an error that names the line, a record type and a fold for the total, a grouped and sorted report, a streaming version that does not hold the file, a command line with subcommands, and a test runner for the parser. Each one is a complete program that builds on this file.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why is money stored as an integer of pence?
Answer
A Double cannot hold 2.50 exactly and a total shows it
Floating point cannot represent most decimal amounts exactly, so the capstone counts pence.
-
What does
linesreturn for a file of four lines?Answer
A list of four strings without the newlines
lines splits on the newline and drops it from each line.
-
Why does the program write the file before reading it?
Answer
So the page needs no separate data file and the run is the same everywhere
The file is part of the program for the lesson, which keeps the example self-contained.
-
What does this program print?
module Main (main) where main :: IO () main = print (length (lines "a\nb\nc"))Answer
3
The three lines a, b and c are three elements.
-
What is the first line of the file?
Answer
The header with the column names
The header names the four columns and the parser skips it.
-
What does this program print?
module Main (main) where main :: IO () main = putStrLn (unlines ["a", "b"])Answer
a and b on two lines
unlines adds a newline to each line and the two lines print.
-
How many fields does a row have?
Answer
Four
Date, item, quantity and pence.
-
What does this program print?
module Main (main) where main :: IO () main = print (take 2 [1, 2, 3 :: Int])Answer
[1,2]
take 2 keeps the first two elements.
-
What does 250 pence mean?
Answer
2.50
One hundred pence make a pound, so 250 pence is two pounds fifty.
-
What does this program print?
module Main (main) where main :: IO () main = print (words "a,b,c")Answer
["a,b,c"]
words splits on whitespace, and the commas are not whitespace.
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