HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulesorted-list-0.2.2.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.

  • 1 type
  • 32 values

Type

1 declaration
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.

Instances9Foldable, IsList, Eq, Ord, Show, Semigroup, …

List conversions

2 declarations

Construction

4 declarations
valuerepeat :: a -> SortedList a
#

An infinite list with all its elements equal to the given argument.

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

1 declaration

Inserting

1 declaration

Deleting

2 declarations

Sublists

6 declarations
valuedrop :: Int -> SortedList a -> SortedList a
#

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

valuesplitAt :: Int -> SortedList a -> (SortedList a, SortedList a)
#

Split a sorted list in two sublists, with the first one having length equal to the given argument, except when the length of the list is less than that.

valuedropWhile :: (a -> Bool) -> SortedList a -> SortedList a
#

Return the suffix remaining after dropping the longest prefix of elements that satisfy the given condition.

Filtering

valuepartition :: (a -> Bool) -> SortedList a -> (SortedList a, SortedList a)
#

O(n). Divide a sorted list into two lists, one with all the elements that satisfy the given predicate, and another list with the rest of elements.

Queries

2 declarations
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.

map function

2 declarations
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.

Unfolding

1 declaration
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

2 declarations
valuereverse :: SortedList a -> SortedList (Down a)
#

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

3 declarations
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.