| Copyright | (c) The University of Glasgow 2001 |
|---|---|
| License | BSD-style (see the file libraries/base/LICENSE) |
| Maintainer | libraries@haskell.org |
| Stability | experimental |
| Portability | portable |
| Safe Haskell | Safe |
| Language | Haskell2010 |
GHC.OldList
Contents
Description
This legacy module provides access to the list-specialised operations ofData.List. This module may go away again in future GHC versions and is provided as transitional tool to access some of the list-specialised operations that had to be generalised due to the implementation of theFoldable/Traversable-in-Prelude Proposal (FTP).
If the operations needed are available inGHC.List, it's recommended to avoid importing this module and useGHC.List instead for now.
Since: 4.8.0.0
(++) :: [a] -> [a] -> [a]infixr 5Source#
Append two lists, i.e.,
[x1, ..., xm] ++ [y1, ..., yn] == [x1, ..., xm, y1, ..., yn][x1, ..., xm] ++ [y1, ...] == [x1, ..., xm, y1, ...]
If the first list is not finite, the result is the first list.
Return all the elements of a list except the last one. The list must be non-empty.
O(n).length returns the length of a finite list as anInt. It is an instance of the more generalgenericLength, the result type of which may be any kind of number.
map :: (a -> b) -> [a] -> [b]Source#
mapf xs is the list obtained by applyingf to each element ofxs, i.e.,
map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn]map f [x1, x2, ...] == [f x1, f x2, ...]
intersperse :: a -> [a] -> [a]Source#
Theintersperse function takes an element and a list and `intersperses' that element between the elements of the list. For example,
>>>intersperse ',' "abcde""a,b,c,d,e"
intercalate :: [a] -> [[a]] -> [a]Source#
intercalatexs xss is equivalent to(. It inserts the listconcat (intersperse xs xss))xs in between the lists inxss and concatenates the result.
>>>intercalate ", " ["Lorem", "ipsum", "dolor"]"Lorem, ipsum, dolor"
transpose :: [[a]] -> [[a]]Source#
Thetranspose function transposes the rows and columns of its argument. For example,
>>>transpose [[1,2,3],[4,5,6]][[1,4],[2,5],[3,6]]
If some of the rows are shorter than the following rows, their elements are skipped:
>>>transpose [[10,11],[20],[],[30,31,32]][[10,20,30],[11,31],[32]]
subsequences :: [a] -> [[a]]Source#
Thesubsequences function returns the list of all subsequences of the argument.
>>>subsequences "abc"["","a","b","ab","c","ac","bc","abc"]
permutations :: [a] -> [[a]]Source#
Thepermutations function returns the list of all permutations of the argument.
>>>permutations "abc"["abc","bac","cba","bca","cab","acb"]
foldl ::forall a b. (b -> a -> b) -> b -> [a] -> bSource#
foldl, applied to a binary operator, a starting value (typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:
foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn
The list must be finite.
foldr :: (a -> b -> b) -> b -> [a] -> bSource#
foldr, applied to a binary operator, a starting value (typically the right-identity of the operator), and a list, reduces the list using the binary operator, from right to left:
foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)
product ::Num a => [a] -> aSource#
Theproduct function computes the product of a finite list of numbers.
iterate' :: (a -> a) -> a -> [a]Source#
'iterate\'' is the strict version ofiterate.
It ensures that the result of each application of force to weak head normal form before proceeding.
replicate ::Int -> a -> [a]Source#
replicaten x is a list of lengthn withx the value of every element. It is an instance of the more generalgenericReplicate, in whichn may be of any integral type.
cycle ties a finite list into a circular one, or equivalently, the infinite repetition of the original list. It is the identity on infinite lists.
unfoldr :: (b ->Maybe (a, b)) -> b -> [a]Source#
Theunfoldr function is a `dual' tofoldr: whilefoldr reduces a list to a summary value,unfoldr builds a list from a seed value. The function takes the element and returnsNothing if it is done producing the list or returnsJust(a,b), in which case,a is a prepended to the list andb 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 afoldr 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]
take ::Int -> [a] -> [a]Source#
taken, applied to a listxs, returns the prefix ofxs of lengthn, orxs itself ifn >:length xs
take 5 "Hello World!" == "Hello"take 3 [1,2,3,4,5] == [1,2,3]take 3 [1,2] == [1,2]take 3 [] == []take (-1) [1,2] == []take 0 [1,2] == []
It is an instance of the more generalgenericTake, in whichn may be of any integral type.
drop ::Int -> [a] -> [a]Source#
dropn xs returns the suffix ofxs after the firstn elements, or[] ifn >:length xs
drop 6 "Hello World!" == "World!"drop 3 [1,2,3,4,5] == [4,5]drop 3 [1,2] == []drop 3 [] == []drop (-1) [1,2] == [1,2]drop 0 [1,2] == [1,2]
It is an instance of the more generalgenericDrop, in whichn may be of any integral type.
splitAt ::Int -> [a] -> ([a], [a])Source#
splitAtn xs returns a tuple where first element isxs prefix of lengthn and second element is the remainder of the list:
splitAt 6 "Hello World!" == ("Hello ","World!")splitAt 3 [1,2,3,4,5] == ([1,2,3],[4,5])splitAt 1 [1,2,3] == ([1],[2,3])splitAt 3 [1,2,3] == ([1,2,3],[])splitAt 4 [1,2,3] == ([1,2,3],[])splitAt 0 [1,2,3] == ([],[1,2,3])splitAt (-1) [1,2,3] == ([],[1,2,3])It is equivalent to( whentake n xs,drop n xs)n is not_|_ (splitAt _|_ xs = _|_).splitAt is an instance of the more generalgenericSplitAt, in whichn may be of any integral type.
takeWhile :: (a ->Bool) -> [a] -> [a]Source#
takeWhile, applied to a predicatep and a listxs, returns the longest prefix (possibly empty) ofxs of elements that satisfyp:
takeWhile (< 3) [1,2,3,4,1,2,3,4] == [1,2]takeWhile (< 9) [1,2,3] == [1,2,3]takeWhile (< 0) [1,2,3] == []
dropWhileEnd :: (a ->Bool) -> [a] -> [a]Source#
ThedropWhileEnd function drops the largest suffix of a list in which the given predicate holds for all elements. For example:
>>>dropWhileEnd isSpace "foo\n""foo"
>>>dropWhileEnd isSpace "foo bar""foo bar"
dropWhileEnd isSpace ("foo\n" ++ undefined) == "foo" ++ undefinedSince: 4.5.0.0
span :: (a ->Bool) -> [a] -> ([a], [a])Source#
span, applied to a predicatep and a listxs, returns a tuple where first element is longest prefix (possibly empty) ofxs of elements that satisfyp and second element is the remainder of the list:
span (< 3) [1,2,3,4,1,2,3,4] == ([1,2],[3,4,1,2,3,4])span (< 9) [1,2,3] == ([1,2,3],[])span (< 0) [1,2,3] == ([],[1,2,3])
break :: (a ->Bool) -> [a] -> ([a], [a])Source#
break, applied to a predicatep and a listxs, returns a tuple where first element is longest prefix (possibly empty) ofxs of elements thatdo not satisfyp and second element is the remainder of the list:
break (> 3) [1,2,3,4,1,2,3,4] == ([1,2,3],[4,1,2,3,4])break (< 9) [1,2,3] == ([],[1,2,3])break (> 9) [1,2,3] == ([1,2,3],[])
stripPrefix ::Eq a => [a] -> [a] ->Maybe [a]Source#
ThestripPrefix function drops the given prefix from a list. It returnsNothing if the list did not start with the prefix given, orJust 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
group ::Eq a => [a] -> [[a]]Source#
Thegroup function takes a list and returns a list of lists such that the concatenation of the result is equal to the argument. Moreover, each sublist in the result contains only equal elements. For example,
>>>group "Mississippi"["M","i","ss","i","ss","i","pp","i"]
It is a special case ofgroupBy, which allows the programmer to supply their own equality test.
isPrefixOf ::Eq a => [a] -> [a] ->BoolSource#
TheisPrefixOf function takes two lists and returnsTrue iff the first list is a prefix of the second.
>>>"Hello" `isPrefixOf` "Hello World!"True
>>>"Hello" `isPrefixOf` "Wello Horld!"False
isSuffixOf ::Eq a => [a] -> [a] ->BoolSource#
TheisSuffixOf function takes two lists and returnsTrue iff the first list is a suffix of the second. The second list must be finite.
>>>"ld!" `isSuffixOf` "Hello World!"True
>>>"World" `isSuffixOf` "Hello World!"False
lookup ::Eq a => a -> [(a, b)] ->Maybe bSource#
lookupkey assocs looks up a key in an association list.
filter :: (a ->Bool) -> [a] -> [a]Source#
filter, applied to a predicate and a list, returns the list of those elements that satisfy the predicate; i.e.,
filter p xs = [ x | x <- xs, p x]
partition :: (a ->Bool) -> [a] -> ([a], [a])Source#
Thepartition function takes a predicate a list and returns the pair of lists of elements which do and do not satisfy the predicate, respectively; i.e.,
partition p xs == (filter p xs, filter (not . p) xs)
>>>partition (`elem` "aeiou") "Hello World!"("eoo","Hll Wrld!")
These functions treat a listxs as a indexed collection, with indices ranging from 0 to.length xs - 1
(!!) :: [a] ->Int -> ainfixl 9Source#
List index (subscript) operator, starting from 0. It is an instance of the more generalgenericIndex, which takes an index of any integral type.
elemIndices ::Eq a => a -> [a] -> [Int]Source#
TheelemIndices function extendselemIndex, by returning the indices of all elements equal to the query element, in ascending order.
>>>elemIndices 'o' "Hello World"[4,7]
findIndices :: (a ->Bool) -> [a] -> [Int]Source#
ThefindIndices function extendsfindIndex, by returning the indices of all elements satisfying the predicate, in ascending order.
>>>findIndices (`elem` "aeiou") "Hello World!"[1,4,7]
zipWith6 :: (a -> b -> c -> d -> e -> f -> g) -> [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g]Source#
zipWith7 :: (a -> b -> c -> d -> e -> f -> g -> h) -> [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g] -> [h]Source#
unzip :: [(a, b)] -> ([a], [b])Source#
unzip transforms a list of pairs into a list of first components and a list of second components.
lines ::String -> [String]Source#
lines breaks a string up into a list of strings at newline characters. The resulting strings do not contain newlines.
Note that after splitting the string at newline characters, the last part of the string is considered a line even if it doesn't end with a newline. For example,
>>>lines ""[]
>>>lines "\n"[""]
>>>lines "one"["one"]
>>>lines "one\n"["one"]
>>>lines "one\n\n"["one",""]
>>>lines "one\ntwo"["one","two"]
>>>lines "one\ntwo\n"["one","two"]
Thus contains at least as many elements as newlines inlines ss.
words ::String -> [String]Source#
words breaks a string up into a list of words, which were delimited by white space.
>>>words "Lorem ipsum\ndolor"["Lorem","ipsum","dolor"]
(\\) ::Eq a => [a] -> [a] -> [a]infix 5Source#
The\\ function is list difference (non-associative). In the result ofxs\\ys, the first occurrence of each element ofys in turn (if any) has been removed fromxs. Thus
(xs ++ ys) \\ xs == ys.
>>>"Hello World!" \\ "ell W""Hoorld!"
It is a special case ofdeleteFirstsBy, which allows the programmer to supply their own equality test.
union ::Eq a => [a] -> [a] -> [a]Source#
Theunion 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 ofunionBy, which allows the programmer to supply their own equality test.
intersect ::Eq a => [a] -> [a] -> [a]Source#
Theintersect 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 ofintersectBy, which allows the programmer to supply their own equality test. If the element is found in both the first and the second list, the element from the first list will be used.
sortOn ::Ord b => (a -> b) -> [a] -> [a]Source#
Sort a list by comparing the results of a key function applied to each element.sortOn f is equivalent tosortBy (comparing f), but has the performance advantage of only evaluatingf once for each element in the input list. This is called the decorate-sort-undecorate paradigm, or Schwartzian transform.
Elements are arranged from from lowest to highest, keeping duplicates in the order they appeared in the input.
>>>sortOn fst [(2, "world"), (4, "!"), (1, "Hello")][(1,"Hello"),(2,"world"),(4,"!")]
Since: 4.8.0.0
insert ::Ord a => a -> [a] -> [a]Source#
Theinsert function takes an element and a list and inserts the element into the list at the first position where it is less than or equal to the next element. In particular, if the list is sorted before the call, the result will also be sorted. It is a special case ofinsertBy, which allows the programmer to supply their own comparison function.
>>>insert 4 [1,2,3,5,6,7][1,2,3,4,5,6,7]
By" operationsBy convention, overloaded functions have a non-overloaded counterpart whose name is suffixed with `By'.
It is often convenient to use these functions together withon, for instance.sortBy (compare `on`fst)
Eq context)The predicate is assumed to define an equivalence.
deleteFirstsBy :: (a -> a ->Bool) -> [a] -> [a] -> [a]Source#
ThedeleteFirstsBy function takes a predicate and two lists and returns the first list with the first occurrence of each element of the second list removed.
intersectBy :: (a -> a ->Bool) -> [a] -> [a] -> [a]Source#
TheintersectBy function is the non-overloaded version ofintersect.
Ord context)The function is assumed to define a total ordering.
maximumBy :: (a -> a ->Ordering) -> [a] -> aSource#
ThemaximumBy 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.
We can use this to find the longest entry of a list:
>>>maximumBy (\x y -> compare (length x) (length y)) ["Hello", "World", "!", "Longest", "bar"]"Longest"
minimumBy :: (a -> a ->Ordering) -> [a] -> aSource#
TheminimumBy 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.
We can use this to find the shortest entry of a list:
>>>minimumBy (\x y -> compare (length x) (length y)) ["Hello", "World", "!", "Longest", "bar"]"!"
generic" operationsThe prefix `generic' indicates an overloaded function that is a generalized version of aPrelude function.
genericLength ::Num i => [a] -> iSource#
ThegenericLength function is an overloaded version oflength. In particular, instead of returning anInt, it returns any type which is an instance ofNum. It is, however, less efficient thanlength.
genericTake ::Integral i => i -> [a] -> [a]Source#
ThegenericTake function is an overloaded version oftake, which accepts anyIntegral value as the number of elements to take.
genericDrop ::Integral i => i -> [a] -> [a]Source#
ThegenericDrop function is an overloaded version ofdrop, which accepts anyIntegral value as the number of elements to drop.
genericSplitAt ::Integral i => i -> [a] -> ([a], [a])Source#
ThegenericSplitAt function is an overloaded version ofsplitAt, which accepts anyIntegral value as the position at which to split.
genericIndex ::Integral i => [a] -> i -> aSource#
ThegenericIndex function is an overloaded version of!!, which accepts anyIntegral value as the index.
genericReplicate ::Integral i => i -> a -> [a]Source#
ThegenericReplicate function is an overloaded version ofreplicate, which accepts anyIntegral value as the number of repetitions to make.
Produced byHaddock version 2.20.0