In languages where cons lists are a major datatype, it is very easy to create a list from last to first by prepending items. When doing processing from some input file, however, one is most likely to encounter items in first to last order.
Do languages like these typically recurse?
(define (parse file)
...
(cons datum (parse file)))
Use append operations at each step? Build the list in reverse order and then reverse it?
Is there some canonical design pattern that can solve the problem that input is typically going to be in the reverse order of the way the list is constructed?
One common way to do this efficiently, at least in Haskell, is using a difference list which permit O(1) snoc
, which is the inverse of cons
(yes, it’s a silly name).
An alternative is not using linked lists in favor of a more appropriate data structure, such as finger trees (Data.Seq
in Haskell, amortized constant time cons
/snoc
) or queues (there are persistent queues with constant time operations, both amortized and worst-case).
Yes, languages that favor recursive data types almost always also favor recursive functions, and for this kind of stream processing it is the obvious solution. (Particularly if the language runtime supports tail recursion elimination, which it usually does.) With recursive processing, the list in fact comes out in the right order, so everything is well.
Appending is often comparably expensive and would make a linear problem effectively into a quadratic one, therefore it is usually an anti-pattern in functional programming languages.
3