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.

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.

  1. 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.

  2. What does lines return 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. How many fields does a row have?

    Answer

    Four

    Date, item, quantity and pence.

  8. 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.

  9. What does 250 pence mean?

    Answer

    2.50

    One hundred pence make a pound, so 250 pence is two pounds fifty.

  10. 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