Text is a packed representation of character strings from the text package. Use it when an application handles substantial textual input or output and String’s linked-list representation becomes a performance concern. Prefer improving algorithms and data structures before replacing String solely for speed. section 6.3.2 section 7.1.1
Import Text from Data.Text, often qualified as T. Its operations replace familiar list-oriented string operations: use T.null and T.length for inspection, and use T.pack and T.unpack at boundaries that still require String. section 7.1.1
ghci> import Data.Text ghci> myString = "Hello Text" ghci> :t pack myString pack myString :: Text ghci> :t unpack (pack myString) unpack (pack myString) :: String
Pack converts a String into Text; unpack converts it back. Checking these types in GHCi makes the boundary explicit. section 7.1.1
Modeling CSV fields
Represent untyped CSV headers and fields as Text, while retaining ordinary lists for columns and rows. This keeps textual cell content packed without changing the surrounding table structure. section 7.1.1 section 7.1.1
Text supports practical file and command-line workflows:
- Join Text fields with T.intercalate to form comma-separated rows, then use Data.Text.IO.writeFile to write the final Text directly. section 8.3
- Convert command-line arguments from IO [String] to IO [Text] by mapping T.pack inside IO with <$>. section 8.4
- Use Text as parser input; T.take, T.length, and T.drop can recognize a fixed prefix while preserving unconsumed input. section 12.1.2
Fixed-text parser
A parser can accept Text and return either an error or a parsed value. A fixed-string parser compares the required prefix, drops it on success, and uses T.unpack only to include expected Text in a String error message. section 12.1.2
