[RFC] Sized Difference List
- Dominant language
- Haskell
- Stars
- 48
- Forks
- 6
- PR merge metrics
- No merged PRs in 30d
Description
Because of how ordinary lists are implemented in Haskell, left-associative appending of such lists is very slow. The following takes much more time than it should:
```haskell
((((l1 ++ l2) ++ l3) ++ l4) ++ ...)
```
Optimal appending order is the following:
```haskell
l1 ++ (l2 ++ (l3 ++ (l4 ++ ...))))
```
There's a trick called _Difference List_ which automatically rearranges list appending to get an optimal order. The trick is representing a list in a different way: instead of storing `[a]`, you store `[a] -> [a]`. It's implemented in the following packages: https://hackage.haskell.org/package/dlist
This technique is very useful if you don't have control of how lists are appending (for example, when appending inside some recursive call).
I thought, that maybe `slist` package could provide a data type like `SDList` (similar to `DList`) — _sized difference list_ to optimize appending. The problem with `DList` is since it represents the list as a function, you can't get any info from this function for free. With `SDList` you should be able to store size and get at list size, so I'm thinking about data type like this:
```haskell
data SDList a = SDList Size (Slist a -> Slist a)
```
Do you think it's worth having such a structure in the library? I guess you don't need to implement bazillion functions for this structure at first, it will be enough to have a few basic functions, because mostly you're interested in appending such difference lists and converting from and to `Slist`.
Contributor guide
Research direction
No repository files, tests, or entry points are named. Start by reviewing the existing slist API and the referenced dlist package, then evaluate the proposed SDList representation and its basic append and conversion operations. The issue does not define acceptance criteria, so the desired API and scope would need maintainer agreement first.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- haskell
- Domain
- data
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100