Module Data.List

This package provides common list functions for the Frege language.

It contains all functions described in chapter 20 "Data.List" of the Haskell 2010 Language Report. Where possible, the code has been ported from public Haskell source code (http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/Data-List.html).

Imports

Table of Content

Definitions

maximumBy ∷ (𝖆→𝖆→Ordering) → [𝖆] → 𝖆

The maximumBy function takes a comparison function and a list and returns the greatest element of the list by the comparison function. The list must be finite and non-empty.

minimumBy ∷ (𝖆→𝖆→Ordering) → [𝖆] → 𝖆

The minimumBy function takes a comparison function and a list and returns the least element of the list by the comparison function. The list must be finite and non-empty.

unfoldr ∷ (b → Maybe (a, b)) → b → [a]

The unfoldr function is a dual to foldr: while foldr reduces a list to a summary value, unfoldr builds a list from a seed value. The function takes the element and returns Maybe.Nothing if it is done producing the list or returns Maybe.Just (a,b), in which case, a is a prepended to the list and b is used as the next element in a recursive call. For example,

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

In some cases, unfoldr can undo a foldr operation:

 unfoldr f' (foldr f z xs) == xs

if the following holds:

 f' (f x y) = Just (x,y)
 f' z       = Nothing

A simple use of unfoldr:

 unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10
  [10,9,8,7,6,5,4,3,2,1]
inits[𝖆] → [[𝖆]]

The inits function returns all initial segments of the argument, shortest first. For example,

 inits "abc" == ["","a","ab","abc"]
tails[𝖆] → [[𝖆]]

The tails function returns all final segments of the argument, longest first. For example,

 tails "abc" == ["abc", "bc", "c",""]
takeUntil ∷ (𝖆→Bool) → [𝖆] → [𝖆]

takeUntil p xs is the same as takeWhile (not • p) xs

dropUntil ∷ (𝖆→Bool) → [𝖆] → [𝖆]

dropUntil p xs is the same as dropWhile (not • p) xs

Consequently, for all lists /xs/

 takeUntil p xs ++ dropUntil p xs == xs
groupEq 𝖆 ⇒ [𝖆] → [[𝖆]]

group xs returns a list of sub-lists made of adjacent equal elements in xs. All sublist are not empty and their concatenation yields again xs.

groupBy ∷ (𝖆→𝖆→Bool) → [𝖆] → [[𝖆]]

groupBy f xs groups by function f instead of (==) that is used by group

elemBy ∷ (𝖆→𝖇→Bool) → 𝖆 → [𝖇]Bool

elemBy f is a more general version of elem that uses /f/ instead of Eq.==.

See also: using

lookupEq 𝖆 ⇒ 𝖆 → [(𝖆, 𝖇)]Maybe 𝖇

lookup a key in an association list

deleteEq 𝖆 ⇒ 𝖆 → [𝖆] → [𝖆]

delete x removes the first occurrence of x from its list argument. For example,

 delete ’a’ "banana" == "bnana"

It is a special case of deleteBy, which allows the programmer to supply their own equality test.

deleteBy ∷ (𝖆→𝖇→Bool) → 𝖆 → [𝖇] → [𝖇]

The deleteBy function behaves like delete, but takes a user-supplied equality predicate.

deleteFirstsBy ∷ (𝖆→𝖇→Bool) → [𝖇][𝖆] → [𝖇]

The deleteFirstsBy function takes a predicate and two lists and returns the first list with the first occurrence of each element of the second list removed.

insertOrd a ⇒ a → [a] → [a]
insertBy ∷ (𝖆→𝖆→Ordering) → 𝖆 → [𝖆] → [𝖆]

The non-overloaded version of insert.

\\Eq a ⇒ [a][a] → [a]
infix  14

The \\ function is list difference (non-associative). In the result of xs \\ ys, the first occurrence of each element of ys in turn (if any) has been removed from xs. Thus

 (xs ++ ys) \\ xs == ys.

It is a special case of deleteFirstsBy, which allows the programmer to supply their own equality test.

unionEq a ⇒ [a] → [a] → [a]

The union function returns the list union of the two lists. For example,

 "dog" `union` "cow" == "dogcw"

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.

It is a special case of unionBy, which allows the programmer to supply their own equality test.

unionBy ∷ (𝖆→𝖆→Bool) → [𝖆] → [𝖆] → [𝖆]

The unionBy function is the non-overloaded version of union.

intersectEq a ⇒ [a] → [a] → [a]

The intersect function takes the list intersection of two lists. For example,

 [1,2,3,4] `intersect` [2,4,6,8] == [2,4]

If the first list contains duplicates, so will the result.

 [1,2,2,3,4] `intersect` [6,4,4,2] == [2,2,4]

It is a special case of intersectBy, which allows the programmer to supply their own equality test.

intersectByListSource 𝖈 ⇒ (𝖇→𝖆→Bool) → 𝖈 𝖇 → [𝖆] → [𝖇]

The intersectBy function is the non-overloaded version of intersect.

uniqueEq 𝖆 ⇒ [𝖆] → [𝖆]

unique removes duplicate elements from an unsorted list, which may or may not be faster than using (uniq • sort)

This function is known as nub in Haskell and Prelude provides this as alias.

However, the following holds

 sort (unique xs) == uniq (sort xs)
nubEq 𝖆 ⇒ [𝖆] → [𝖆]

Alias for unique

nubBy ∷ (𝖆→𝖆→Bool) → [𝖆] → [𝖆]

Alias for uniqueBy

uniqueBy ∷ (𝖆→𝖆→Bool) → [𝖆] → [𝖆]

uniqueBy f is a more general form of unique, but uses f instead of Eq.== to decide whether equal elements are contained in the list.

The following holds:

 sortBy (comparing f) (uniqueBy (using f) xs) == uniqBy (using f) (sortBy (comparing f) xs)
uniqEq 𝖆 ⇒ [𝖆] → [𝖆]

uniq removes adjacent equal elements from a list

 uniq [1, 2, 2, 3, 2] = [1, 2, 3, 2]

This is most useful on sorted lists to remove duplicates. For unsorted lists use unique

uniqBy ∷ (𝖆→𝖆→Bool) → [𝖆] → [𝖆]

uniqBy f is a variant of uniq that uses f instead of Eq.==. In the result, there are no two adjacent elements x and y where the relation y `f` x holds.

This is most useful on sorted lists with projection functions that compare parts of the value for equality. See also using.

 uniqBy (using fst) [(1, 1), (2, 2), (2, 3), (3, 4), (2, 5)]
   = uniqBy (\a\b -> fst a == fst b) [(1, 1), (2, 2), (2, 3), (3, 4), (2, 5)]
   = [(1, 1), (2, 2), (3, 4), (2, 5)]

The example shows that the first of adjacent, equal comparing elements is retained.

partitioned ∷ (𝖆→Bool) → [𝖆] → ([𝖆], [𝖆])

partitioned p xs splits xs in 2 lists and returns them as a tuple (xs1, xs2), such that xs1 contains all elements of xs that satisfy predicate p and xs2 contains those that do not.

The order of the elements of xs is reversed in the results. The argument must be finite, it is processed in a tail recursive loop.

See also partition, which is lazy and works on infinite lists, but may be slower on finite lists because it processes the argument twice.

The following is true for all finite lists xs

 let ps = partitionR p xs
 in    all p (fst ps)
    && (not • any p) (snd ps)
    && length (fst ps) + length (snd ps) == length xs
    && all (`elem` xs) (fst ps)
    && all (`elem` xs) (snd ps)
    && all (\x -> x `elem` fst ps || x `elem` snd ps) xs
partition ∷ (𝖆→Bool) → [𝖆] → ([𝖆], [𝖆])

A variant of partition that satisfies the Haskell 2010 specification. When the order of the results is irrelevant or one actually wants the results reversed, consider the more efficient partitioned.

intercalate ∷ [a] → [[a]] → [a]

intercalate xs xss is equivalent to concat (intersperse xs xss)

zip4[𝖆] → [𝖇] → [𝖈] → [𝖉] → [(𝖆, 𝖇, 𝖈, 𝖉)]

zip4 zips 4 lists in the same way as zip does it.

unzip4[(𝖆, 𝖇, 𝖈, 𝖉)] → ([𝖆], [𝖇], [𝖈], [𝖉])

unzip4 unzips a list of quadrupels and returns a quadrupel of lists.

zipWith4 ∷ (𝖇→𝖈→𝖉→𝖊→𝖆) → [𝖇] → [𝖈] → [𝖉] → [𝖊] → [𝖆]

zipWith4 /f/ zips 4 lists with function /f/ instead of the standard (,,,) that is used by zip4

zip5[𝖆] → [𝖇] → [𝖈] → [𝖉] → [𝖊] → [(𝖆, 𝖇, 𝖈, 𝖉, 𝖊)]

zip5 zips 5 lists in the same way as zip does it.

unzip5[(𝖆, 𝖇, 𝖈, 𝖉, 𝖊)] → ([𝖆], [𝖇], [𝖈], [𝖉], [𝖊])

unzip5 unzips a list of quintuples and returns a quintuple of lists.

zipWith5 ∷ (𝖇→𝖈→𝖉→𝖊→𝖋→𝖆) → [𝖇] → [𝖈] → [𝖉] → [𝖊] → [𝖋] → [𝖆]

zipWith5 /f/ zips 5 lists with function /f/ instead of the standard (,,,,) that is used by zip5

zip6[𝖆] → [𝖇] → [𝖈] → [𝖉] → [𝖊] → [𝖋] → [(𝖆, 𝖇, 𝖈, 𝖉, 𝖊, 𝖋)]

zip6 zips 6 lists in the same way as zip does it.

unzip6[(𝖆, 𝖇, 𝖈, 𝖉, 𝖊, 𝖋)] → ([𝖆], [𝖇], [𝖈], [𝖉], [𝖊], [𝖋])

unzip6 unzips a list of sextuples and returns a sextuple of lists.

zipWith6 ∷ (𝖇→𝖈→𝖉→𝖊→𝖋→𝖌→𝖆) → [𝖇] → [𝖈] → [𝖉] → [𝖊] → [𝖋] → [𝖌] → [𝖆]

zipWith6 /f/ zips 6 lists with function /f/ instead of the standard (,,,,,) that is used by zip6

zip7[𝖆] → [𝖇] → [𝖈] → [𝖉] → [𝖊] → [𝖋] → [𝖌] → [(𝖆, 𝖇, 𝖈, 𝖉, 𝖊, 𝖋, 𝖌)]

zip7 zips 7 lists in the same way as zip does it.

unzip7[(𝖆, 𝖇, 𝖈, 𝖉, 𝖊, 𝖋, 𝖌)] → ([𝖆], [𝖇], [𝖈], [𝖉], [𝖊], [𝖋], [𝖌])

unzip7 unzips a list of septuples and returns a septuple of lists.

zipWith7 ∷ (𝖇→𝖈→𝖉→𝖊→𝖋→𝖌→𝖍→𝖆) → [𝖇] → [𝖈] → [𝖉] → [𝖊] → [𝖋] → [𝖌] → [𝖍] → [𝖆]

zipWith7 /f/ zips 7 lists with function /f/ instead of the standard (,,,,,,) that is used by zip7

sortByListSource 𝖇 ⇒ (𝖆→𝖆→Ordering) → 𝖇 𝖆 → [𝖆]

sortBy f xs is a stable sort (merge sort), it uses /f/ to decide the order of elements. If a `f` b is Ordering.Lt or Eq, then /a/ comes before /b/, otherwise /b/ comes before /a/.

see also comparing, descending

merge ∷ (𝖆→𝖆→Ordering) → [𝖆] → [𝖆] → [𝖆]

Merge two lists, taking elements from the left as long as they are not Ordering.Gt (according to the passed comparison function) than the head element from the right.

Makes most sense when the lists are sorted by the same criteria.

sort ∷ (Ord 𝖇, ListSource 𝖆) ⇒ 𝖆 𝖇 → [𝖇]

Standard sort uses operator Ord.<=> and demands that the type of the list elements is an instance of Ord

transpose[[a]] → [[a]]

The transpose function transposes the rows and columns of its argument.

For example,

 transpose [[1,2,3],[4,5,6]] == [[1,4],[2,5],[3,6]]   
subsequences ∷ [𝖆] → [[𝖆]]

The subsequences function returns the list of all subsequences of the argument.

 subsequences "abc" == ["","a","b","ab","c","ac","bc","abc"]   
nonEmptySubsequences[𝖆] → [[𝖆]]

The nonEmptySubsequences function returns the list of all subsequences of the argument, except for the empty list.

 nonEmptySubsequences "abc" == ["a","b","ab","c","ac","bc","abc"]
permutations ∷ [𝖆] → [[𝖆]]

The permutations function returns the list of all permutations of the argument.

 permutations "abc" == ["abc","bac","cba","bca","cab","acb"]   
mapAccumL ∷ (acc → x → (acc, y)) → acc → [x] → (acc, [y])

The mapAccumL function behaves like a combination of map and fold; it applies a function to each element of a list, passing an accumulating parameter from left to right, and returning a final value of this accumulator together with the new list.

mapAccumR ∷ (acc → x → (acc, y)) → acc → [x] → (acc, [y])

The mapAccumR function behaves like a combination of map and foldr; it applies a function to each element of a list, passing an accumulating parameter from right to left, and returning a final value of this accumulator together with the new list.

stripPrefixEq 𝖆 ⇒ [𝖆] → [𝖆] → Maybe [𝖆]

The stripPrefix function drops the given prefix from a list. It returns Maybe.Nothing if the list did not start with the prefix given, or Maybe.Just the list after the prefix, if it does.

 stripPrefix "foo" "foobar" -> Just "bar"
 stripPrefix "foo" "foo" -> Just ""
 stripPrefix "foo" "barfoo" -> Nothing
 stripPrefix "foo" "barfoobaz" -> Nothing
isPrefixOfEq 𝖆 ⇒ [𝖆] → [𝖆] → Bool

The isPrefixOf function takes two lists and returns true iff the first list is a prefix of the second.

isSuffixOfEq a ⇒ [a] → [a] → Bool

The isSuffixOf function takes two lists and returns true iff the first list is a suffix of the second. Both lists must be finite.

isInfixOfEq a ⇒ [a][a]Bool

The isInfixOf function takes two lists and returns true iff the first list is contained, wholly and intact, anywhere within the second.

Example:

 isInfixOf "Haskell" "I really like Haskell." == true
 isInfixOf "Ial" "I really like Haskell." == false
elemIndexEq a ⇒ a → [a] → Maybe Int

The elemIndex function returns the index of the first element in the given list which is equal (by Eq.==) to the query element, or Maybe.Nothing if there is no such element.

elemIndicesEq a ⇒ a → [a] → [Int]

The elemIndices function extends elemIndex, by returning the indices of all elements equal to the query element, in ascending order.

find ∷ (a → Bool) → [a] → Maybe a

The find function takes a predicate and a list and returns the first element in the list matching the predicate, or Maybe.Nothing if there is no such element.

findIndex ∷ (a → Bool) → [a] → Maybe Int

The findIndex function takes a predicate and a list and returns the index of the first element in the list satisfying the predicate, or Maybe.Nothing if there is no such element.

findIndices ∷ (a → Bool) → [a] → [Int]

The findIndices function extends findIndex, by returning the indices of all elements satisfying the predicate, in ascending order.

genericLengthNum i ⇒ [b] → i

The genericLength function is an overloaded version of ListView.length. In particular, instead of returning an Int, it returns any type which is an instance of Num. It is, however, less efficient than ListView.length.

genericTakeIntegral i ⇒ i → [a] → [a]

The genericTake function is an overloaded version of ListView.take, which accepts any Integral value as the number of elements to take.

genericDropIntegral i ⇒ i → [a] → [a]

The genericDrop function is an overloaded version of ListView.drop, which accepts any Integral value as the number of elements to drop.

genericSplitAtIntegral i ⇒ i → [b] → ([b], [b])

The genericSplitAt function is an overloaded version of splitAt, which accepts any Integral value as the position at which to split.

genericIndexIntegral a ⇒ [b] → a → b

The genericIndex function is an overloaded version of !!, which accepts any Integral value as the index.

genericReplicateIntegral i ⇒ i → a → [a]

The genericReplicate function is an overloaded version of replicate, which accepts any Integral value as the number of repetitions to make.

Functions and Values by Type

(a → Bool) → [a] → Maybe Int

findIndex

(a → Bool) → [a] → Maybe a

find

(a → Bool) → [a] → [Int]

findIndices

(𝖆→𝖆→Bool) → [𝖆] → [𝖆] → [𝖆]

unionBy

(𝖆→𝖆→Bool) → [𝖆] → [[𝖆]]

groupBy

(𝖆→𝖆→Bool) → [𝖆] → [𝖆]

uniqBy, uniqueBy

(𝖆→𝖆→Ordering) → [𝖆] → [𝖆] → [𝖆]

merge

(𝖆→𝖆→Ordering) → [𝖆] → 𝖆

maximumBy, minimumBy

(𝖆→𝖆→Ordering) → 𝖆 → [𝖆] → [𝖆]

insertBy

(𝖆→Bool) → [𝖆] → ([𝖆], [𝖆])

partition, partitioned

(𝖆→Bool) → [𝖆] → [𝖆]

dropUntil, takeUntil

[[a]] → [[a]]

transpose

[a] → [[a]] → [a]

intercalate

[𝖆] → [[𝖆]]

inits, nonEmptySubsequences, permutations, subsequences, tails

Eq a ⇒ [a] → [a] → [a]

\\, intersect, union

Eq a ⇒ [a] → [a] → Bool

isInfixOf, isSuffixOf

Eq a ⇒ a → [a] → Maybe Int

elemIndex

Eq a ⇒ a → [a] → [Int]

elemIndices

Eq 𝖆 ⇒ [𝖆] → [𝖆] → Maybe [𝖆]

stripPrefix

Eq 𝖆 ⇒ [𝖆] → [𝖆] → Bool

isPrefixOf

Eq 𝖆 ⇒ [𝖆] → [[𝖆]]

group

Eq 𝖆 ⇒ [𝖆] → [𝖆]

uniq, unique

Eq 𝖆 ⇒ 𝖆 → [𝖆] → [𝖆]

delete

Ord a ⇒ a → [a] → [a]

insert

(b → Maybe (a, b)) → b → [a]

unfoldr

(𝖆→𝖇→Bool) → [𝖇] → [𝖆] → [𝖇]

deleteFirstsBy

(𝖆→𝖇→Bool) → 𝖆 → [𝖇] → [𝖇]

deleteBy

(𝖆→𝖇→Bool) → 𝖆 → [𝖇] → Bool

elemBy

Eq 𝖆 ⇒ 𝖆 → [(𝖆, 𝖇)] → Maybe 𝖇

lookup

Integral a ⇒ [b] → a → b

genericIndex

Integral i ⇒ i → [a] → [a]

genericDrop, genericTake

Integral i ⇒ i → [b] → ([b], [b])

genericSplitAt

Integral i ⇒ i → a → [a]

genericReplicate

Num i ⇒ [b] → i

genericLength

(Ord 𝖇, ListSource 𝖆) ⇒ 𝖆 𝖇 → [𝖇]

sort

ListSource 𝖇 ⇒ (𝖆→𝖆→Ordering) → 𝖇 𝖆 → [𝖆]

sortBy

(acc → x → (acc, y)) → acc → [x] → (acc, [y])

mapAccumL, mapAccumR

ListSource 𝖈 ⇒ (𝖇→𝖆→Bool) → 𝖈 𝖇 → [𝖆] → [𝖇]

intersectBy

[(𝖆, 𝖇, 𝖈, 𝖉)] → ([𝖆], [𝖇], [𝖈], [𝖉])

unzip4

[𝖆] → [𝖇] → [𝖈] → [𝖉] → [(𝖆, 𝖇, 𝖈, 𝖉)]

zip4

(𝖇→𝖈→𝖉→𝖊→𝖆) → [𝖇] → [𝖈] → [𝖉] → [𝖊] → [𝖆]

zipWith4

[(𝖆, 𝖇, 𝖈, 𝖉, 𝖊)] → ([𝖆], [𝖇], [𝖈], [𝖉], [𝖊])

unzip5

[𝖆] → [𝖇] → [𝖈] → [𝖉] → [𝖊] → [(𝖆, 𝖇, 𝖈, 𝖉, 𝖊)]

zip5

(𝖇→𝖈→𝖉→𝖊→𝖋→𝖆) → [𝖇] → [𝖈] → [𝖉] → [𝖊] → [𝖋] → [𝖆]

zipWith5

[(𝖆, 𝖇, 𝖈, 𝖉, 𝖊, 𝖋)] → ([𝖆], [𝖇], [𝖈], [𝖉], [𝖊], [𝖋])

unzip6

[𝖆] → [𝖇] → [𝖈] → [𝖉] → [𝖊] → [𝖋] → [(𝖆, 𝖇, 𝖈, 𝖉, 𝖊, 𝖋)]

zip6

(𝖇→𝖈→𝖉→𝖊→𝖋→𝖌→𝖆) → [𝖇] → [𝖈] → [𝖉] → [𝖊] → [𝖋] → [𝖌] → [𝖆]

zipWith6

[(𝖆, 𝖇, 𝖈, 𝖉, 𝖊, 𝖋, 𝖌)] → ([𝖆], [𝖇], [𝖈], [𝖉], [𝖊], [𝖋], [𝖌])

unzip7

[𝖆] → [𝖇] → [𝖈] → [𝖉] → [𝖊] → [𝖋] → [𝖌] → [(𝖆, 𝖇, 𝖈, 𝖉, 𝖊, 𝖋, 𝖌)]

zip7

(𝖇→𝖈→𝖉→𝖊→𝖋→𝖌→𝖍→𝖆) → [𝖇] → [𝖈] → [𝖉] → [𝖊] → [𝖋] → [𝖌] → [𝖍] → [𝖆]

zipWith7

Valid HTML 4.01 Strict