HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Modulesorted-list-0.3.1.0Haskell2010

Data.SortedList

This module defines a type for sorted lists, together with several functions to create and use values of that type. Many operations are optimized to take advantage of the list being sorted.

It is recommended to import this module qualified.

Contents
  1. Type
  2. List conversions
  3. Construction
  4. Deconstruction
  5. Inserting
  6. Deleting
  7. Sublists
  8. Filtering
  9. Queries
  10. Functor functions
  11. Applicative functions
  12. Traversable functions
  13. Unfolding
  14. Others
  15. Set operations
  16. Unsafe
  17. Testing
Synopsis

Type

newtypenewtype SortedList a

Type of sorted lists. Any (non-bottom) value of this type is a sorted list. Use the Monoid instance to merge sorted lists.

Instances10Foldable, IsList, Eq, Ord, Show, Semigroup, …

List conversions

Construction

valueiterate :: Ord a => (a -> a) -> a -> SortedList a

Create a sorted list by repeatedly applying the same function to an element, until the image by that function is stricly less than its argument. In other words:

iterate f x = [x, f x, f (f x), ... ]

With the list ending whenever f (f (... (f (f x)) ...)) < f (... (f (f x)) ...). If this never happens, the list will be infinite.

By definition:

iterate f = unfoldr (\x -> Just (x, f x))

Deconstruction

Inserting

Deleting

Sublists

valuedrop :: Int -> SortedList a -> SortedList a

Drop the given number of elements from a sorted list, starting from the smallest and following ascending order.

Filtering

Queries

valueelemOrd :: Ord a => a -> SortedList a -> Bool

O(n). An efficient implementation of elem, using the Ord instance of the elements in a sorted list. It only traverses the whole list if the requested element is greater than all the elements in the sorted list.

Functor functions

valuemap :: Ord b => (a -> b) -> SortedList a -> SortedList b

Map a function over all the elements of a sorted list. Note that map will hang if the argument is an infinite list.

Even though SortedList can't be made an instance of Functor, map does hold the Functor laws (for finite lists). We can't however write an instance because of the Ord instance requirement on the type of the elements of the result list. Therefore, while SortedList is not a functor type in general, it is when restricted to elements of orderable types (for finite lists).

The complexity range goes from O(n) (if the function is monotonically increasing) to O(n²) (if the function is monotonically decreasing). These are the best and worst case scenarios. We provide an alternative (mapDec) where monotonically decreasing functions are the best case scenario.

valuemapDec :: Ord b => (a -> b) -> SortedList a -> SortedList b

Just like map, but favoring functions that are monotonically decreasing instead of those that are monotonically increasing.

Applicative functions

Traversable functions

valuetraverse
  1. :: (Monad m, Ord b)
  2. => a -> m b
  3. -> SortedList a
  4. -> m (SortedList b)

Traverse a sorted list with a function that returns in a monad. The same performance observations apply here as in map, as changing the values may involve reordering the list.

Unfolding

valueunfoldr :: Ord a => (b -> Maybe (a, b)) -> b -> SortedList a

Dual (sort of) to foldr for sorted lists. It builds a sorted list from a generator function and an initial element. The generator function is applied to the initial element, and then it will produce either Nothing - meaning that the list building must stop - or Just applied to the value that is going to be added to the list, and a new accumulator to be fed to the generator function. The list building will stop prematurely if the generator function happens to create an element for the list that is strictly smaller than the previous value.

Others

O(n). Reverse a sorted list. The result uses Down, thus it is a sorted list as well. The following equality holds for any sorted list xs:

map Down xs = reverse xs

Only available from base version 4.6.0.0.

Set operations

valueunion :: Ord a => SortedList a -> SortedList a -> SortedList a

Union of sorted lists. Duplicates, and elements of the first list, are removed from the the second list, but if the first list contains duplicates, so will the result.

Unsafe

Convert a regular list into a sorted list without sorting it. By using this function, you are responsible of ensuring that the input is already sorted. If it's not, things will break badly.

Testing