spl / dlist

Difference lists in Haskell
https://hackage.haskell.org/package/dlist
BSD 3-Clause "New" or "Revised" License
65 stars 15 forks source link

Provide access to last #20

Closed yongqli closed 9 years ago

yongqli commented 9 years ago

It should be possible to access last in O(1).

spl commented 9 years ago

I'm assuming you mean the analog to the list last function. I don't see how you can do last in O(1). Even head and tail are O(n). If you can implement it, I'd certainly consider a pull request.

yongqli commented 9 years ago

Hmm, I guess we can't pattern match on the last snoc, so it's not possible with this definition.

spl commented 9 years ago

Since a DList is represented as a function, primarily to get the “free” append via function composition, it is really a “one-way” type for collecting a bunch of appends and dealing them out in the end when you want to reconstruct the desired list. Thus, most of the DList functions that provide access to elements of the implied list are really convenience functions on top of toList.

You may be interested in Data.Sequence for its O(1) viewr.