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.
- readMaybe returns a Maybe, so a non-numeric quantity is a Left and not a crash.
- The pattern [date, item, qty, pence] matches only a list of four fields; everything else takes the error branch.
- The error is a String here and a typed error in the second capstone, which is the difference between a report and an API.
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
A new set of questions in a new order every time. Choose one answer for each, then check. What does Cuts the text at every comma and keeps the pieces splitOn returns the list of pieces between the separators. Why does the parser return 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. What does Nothing readMaybe is the total version of read and returns Nothing on failure. What does this program print?
[Just 1,Nothing] The first parses and the second gives Nothing. Which pattern matches exactly four fields? A list pattern of four elements matches a list of four and nothing else. What does this program print?
3 words gives three strings and length counts them. Why is the line number passed to the parser? The caller decides what a line means and the message names it The parser knows the fields and the caller knows the position. What does this program print?
["a","b"] The text is cut at the comma into two pieces. What is the difference between the tuple and the record here? The record gives the fields names for the reader The representation is the same and the names make the report code readable. What does this program print?
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 IsaevQuestions
T.splitOn (T.pack ",") do?Answer
Either String Row?Answer
readMaybe give for a non-numeric quantity?Answer
module Main (main) where
import Text.Read (readMaybe)
main :: IO ()
main = print (map readMaybe ["1", "x"] :: [Maybe Int])Answer
Answer
[date, item, qty, pence]module Main (main) where
main :: IO ()
main = print (length (words "a b c"))Answer
Answer
module Main (main) where
import qualified Data.Text as T
main :: IO ()
main = print (T.splitOn (T.pack ",") (T.pack "a,b"))Answer
Answer
module Main (main) where
import Text.Read (readMaybe)
main :: IO ()
main = print (readMaybe "42" :: Maybe Int)Answer