8

Some months ago I read somewhere of an efficient approach for appending and prepending lists to other lists in O(1) by representing them with function compositions that, once evaluated, build in O(n) the resulting list.

Unfortunately I cannot remember the source of this article or (if existing) the name of this technique/approach. Do you have references about it, please?

Riccardo T.
  • 8,907
  • 5
  • 38
  • 78
  • 2
    The (probable) first appearance was in a paper by John Hughes "A Novel Representation of Lists and its Application to the Function 'Reverse'" (I believe they were folklore before this) - hence they are also called _Hughes Lists_. Note they only support efficient **construction** - the DList package on Hackage has an API supporting _introspection_, but to implement introspection you have to metamorph into a regular list and back out again. This is inefficient - if you want introspection you want a different structure. – stephen tetley Jul 04 '12 at 16:27
  • It is a fair enough tradeoff when introspection is not needed. Thank you for pointing it out. I'm not searching for a data structure to use right now however, I just wanted to study the implementation for difference/Hughes lists. – Riccardo T. Jul 04 '12 at 16:55

2 Answers2

11

The data structure is called a difference list (or DList for short). You can find a default implementation of it in a library available on Hackage.

As you mentioned, a full description can be gathered from a chapter in Real World Haskell on the subject.

dflemstr
  • 25,947
  • 5
  • 70
  • 105
  • Great, thanks! That's exactly what I was searching for. Googling a little bit for `DList`, I found out that I was actually searching for the chapter 13 of "Real World Haskell" by Don Stewart... here is the full explanation to how those lists work: http://book.realworldhaskell.org/read/data-structures.html#data.dlist Could you put a link to the book in the answer as a future reference, please? Thanks – Riccardo T. Jul 04 '12 at 13:49
  • It is also mentioned in LYAH: http://learnyouahaskell.com/for-a-few-monads-more, chapter "Difference lists" – efie Jul 04 '12 at 14:04
  • 1
    @Riccardo the original difference list was of course born in Prolog, and consists of nothing more than a singly-linked list which also carries around the pointer to its last cons cell, so it can be efficiently extended. In non-backtracking Prolog as in Haskell, once a pointer is set it can't be changed. It is easily implemented in C and can be made appear immutable by adding another data field, *length*, to the data structure, so no access past that point is allowed. Extending it would make a copy with new length but same underlying list. (implementers, NB!) :) – Will Ness Jul 05 '12 at 15:20
  • @WillNess: I used difference lists several times in Prolog, but I did not relate them to this implementation until your precisation. Thanks! – Riccardo T. Jul 05 '12 at 15:25
  • @Riccardo you're welcome. :) with all the great mystery as they are usually presented in Prolog, it took me some time to realize what they really are. Open-ended list is a promise to prepend, and a promise in Haskell is a function. Here's another one for you: corecursion == tail-recursion modulo cons. At least I think so. :) Knot-tying is a breeze in Prolog, as it is *explicitly* set-once, etc. – Will Ness Jul 05 '12 at 16:03
1

You must be thinking of ShowS and friends from the Prelude. See here.

jberryman
  • 16,334
  • 5
  • 42
  • 83