I’m writing some code that needs to frequently append to the end of a list. I know that using “++” is inefficient. So instead I build up the list backwards by appending to the head, and then reverse it when I’m done. I gather that this a common beginner tactic.
I would rather build it up in the correct order to begin with – but that means switching to a new data structure. I’m considering using Data.Sequence or Data.DList for my container. My list consists of strict int pairs, and I don’t need random access to it. What are the relative merits of Data.Sequence and Data.DList, and are there other containers I should consider?
Whether to use
Data.SequenceorDListdepends on how you are going to be using the resulting list.DListis great when you are building up a sequence, say in aWritercomputation, to convert to a list at the end and use it. However, if you need to use the intermediate results, like, say:then
DListis pretty bad, because it needs to recompute the spine each time.Data.Sequenceis a better choice in this situation.Data.Sequenceis also better if you need to remove elements from the sequence.But maybe you don’t even need to make this decision. Reversing lists at the end of a computation is common in strict functional languages like ML and Scheme, but not in Haskell. Take, for example, these two ways of writing
map:In a strict language,
map_iiwould be horrible because it uses linear stack space, whereasmap_iis tail recursive. But because Haskell is lazy,map_iis the inefficient one.map_iican consume one element of the input and yield one element of the output, whereasmap_iconsumes the whole input before yielding any output.Tail recursion isn’t the holy grail of efficient implementation in Haskell. When producing a data structure like a list, you actually want to be co-recursive; that is, make the recursive call underneath an application of a constructor (eg.
f x : map_ii f xsabove).So if you find yourself reversing after a tail-recursive function, see if you can factor the whole lot into a corecursive function.