Capstone A: parsing a line

Splitting a line

A row is four fields separated by commas, and Data.Text.splitOn is the function that cuts it. The lesson on text used the same function; here the pieces become a record:

data Row = Row { rowDate :: String , rowItem :: String , rowQty :: Int , rowPence :: Int } deriving (Show)

The parser returns Either String Row, so a bad line is a value and not an exception, and the error says which line it was. A CSV with a quoted comma would need a real parser, which is the second capstone; this file has no quotes and the split is enough.

The parser

module Main (main) where import qualified Data.Text as T import Text.Read (readMaybe) data Row = Row { rowDate :: String , rowItem :: String , rowQty :: Int , rowPence :: Int } deriving (Show) parseLine :: Int -> T.Text -> Either String Row parseLine n line = case T.splitOn (T.pack ",") line of [date, item, qty, pence] -> case (readMaybe (T.unpack qty), readMaybe (T.unpack pence)) of (Just q, Just p) -> Right (Row (T.unpack date) (T.unpack item) q p) _ -> Left ("line " ++ show n ++ ": bad number") _ -> Left ("line " ++ show n ++ ": expected 4 fields") main :: IO () main = do let rows = [ "2026-11-01,tea,3,250" , "2026-11-02,cake,1,400" , "bad,line" ] mapM_ print (zipWith parseLine [1 ..] (map T.pack rows))

ghc -Wall -O1 -o parse parse.hs
./parse

Right (Row {rowDate = "2026-11-01", rowItem = "tea", rowQty = 3, rowPence = 250})
Right (Row {rowDate = "2026-11-02", rowItem = "cake", rowQty = 1, rowPence = 400})
Left "line 3: expected 4 fields"

The error that names the line

The number is passed to the parser and not computed inside it, so the caller decides what a line number means: a line of a file, a record of a stream, a case of a test. The output shows the two good rows and the third one as a Left with the reason.

Why a record and not a tuple

A tuple of four would hold the same values and the report would be (String, String, Int, Int) everywhere. The record gives the fields names, and a reader of the report code sees rowQty row instead of

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does T.splitOn (T.pack ",") do?

    Answer

    Cuts the text at every comma and keeps the pieces

    splitOn returns the list of pieces between the separators.

  2. Why does the parser return Either String Row?

    Answer

    A bad line is a value with a reason instead of an exception

    Either carries the error in the type, so the caller must handle it.

  3. What does readMaybe give for a non-numeric quantity?

    Answer

    Nothing

    readMaybe is the total version of read and returns Nothing on failure.

  4. What does this program print? module Main (main) where import Text.Read (readMaybe) main :: IO () main = print (map readMaybe ["1", "x"] :: [Maybe Int])

    Answer

    [Just 1,Nothing]

    The first parses and the second gives Nothing.

  5. Which pattern matches exactly four fields?

    Answer

    [date, item, qty, pence]

    A list pattern of four elements matches a list of four and nothing else.

  6. What does this program print? module Main (main) where main :: IO () main = print (length (words "a b c"))

    Answer

    3

    words gives three strings and length counts them.

  7. Why is the line number passed to the parser?

    Answer

    The caller decides what a line means and the message names it

    The parser knows the fields and the caller knows the position.

  8. What does this program print? module Main (main) where import qualified Data.Text as T main :: IO () main = print (T.splitOn (T.pack ",") (T.pack "a,b"))

    Answer

    ["a","b"]

    The text is cut at the comma into two pieces.

  9. What is the difference between the tuple and the record here?

    Answer

    The record gives the fields names for the reader

    The representation is the same and the names make the report code readable.

  10. What does this program print? module Main (main) where import Text.Read (readMaybe) main :: IO () main = print (readMaybe "42" :: Maybe Int)

    Answer

    Just 42

    The value parses, so the result is Just 42.

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