← All languages
Haskell function of the day
Random

unlines

Join lines into a single string.

Description

unlines takes a list of strings and joins them by appending a newline character after each one, producing a single string. Its type signature is [String] -> String. Note that unlines appends a newline after every string, including the last one, so the result always ends with a newline.

unlines is the approximate inverse of lines, though the relationship is not perfectly symmetric due to trailing newline handling. Specifically, lines (unlines xs) may not equal xs in all edge cases. However, for typical use of joining lines of text, unlines behaves as expected.

unlines is commonly used when generating multi-line output, such as building configuration files, formatting reports, or transforming text line by line. The pattern unlines (map f (lines text)) is a standard idiom for applying a transformation to each line of a text.

Arguments

NameDescriptionOptional
ls A list of Strings to join with newlines. No

Example

unlines ["hello", "world"]    -- "hello\nworld\n"
unlines ["one"]                -- "one\n"
unlines []                     -- ""

Reference