Safe Haskell | Safe-Inferred |
---|

Functions for dealing with lists.

- module Data.List
- module Data.List.Split
- module Data.List.Extras
- list :: b -> ([a] -> b) -> [a] -> b
- unionOf :: Eq a => [[a]] -> [a]
- for :: [a] -> (a -> b) -> [b]
- lastToMaybe :: [a] -> Maybe a
- firstOr :: a -> [a] -> a
- maxList :: (Num t, Ord t) => [t] -> t
- powerslice :: [a] -> [[a]]
- merge :: Ord a => [a] -> [a] -> [a]
- mergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
- hasAny :: Eq a => [a] -> [a] -> Bool
- takeWhileList :: ([a] -> Bool) -> [a] -> [a]
- dropWhileList :: ([a] -> Bool) -> [a] -> [a]
- spanList :: ([a] -> Bool) -> [a] -> ([a], [a])
- keysAL :: [(key, a)] -> [key]
- valuesAL :: [(a, value)] -> [value]
- breakList :: ([a] -> Bool) -> [a] -> ([a], [a])
- replace :: Eq a => [a] -> [a] -> [a] -> [a]
- genericJoin :: Show a => String -> [a] -> String
- addToAL :: Eq key => [(key, elt)] -> key -> elt -> [(key, elt)]
- delFromAL :: Eq key => [(key, a)] -> key -> [(key, a)]
- hasKeyAL :: Eq a => a -> [(a, b)] -> Bool
- flipAL :: (Eq key, Eq val) => [(key, val)] -> [(val, [key])]
- strToAL :: (Read a, Read b) => String -> [(a, b)]
- strFromAL :: (Show a, Show b) => [(a, b)] -> String
- countElem :: Eq a => a -> [a] -> Int
- elemRIndex :: Eq a => a -> [a] -> Maybe Int
- alwaysElemRIndex :: Eq a => a -> [a] -> Int
- seqList :: [a] -> [a]

# Re-exports

module Data.List

module Data.List.Split

module Data.List.Extras

# List operations

list :: b -> ([a] -> b) -> [a] -> bSource

When a list is non-null, pass it to a function, otherwise use the default.

lastToMaybe :: [a] -> Maybe aSource

Maybe get the last element in the list.

powerslice :: [a] -> [[a]]Source

Essentially a powerset but retaining contiguously ordererd subsets.

merge :: Ord a => [a] -> [a] -> [a]Source

Merge two sorted lists into a single, sorted whole.

Example:

merge [1,3,5] [1,2,4,6] -> [1,1,2,3,4,5,6]

QuickCheck test property:

prop_merge xs ys = merge (sort xs) (sort ys) == sort (xs ++ ys) where types = xs :: [Int]

mergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]Source

Merge two sorted lists using into a single, sorted whole, allowing the programmer to specify the comparison function.

QuickCheck test property:

prop_mergeBy xs ys = mergeBy cmp (sortBy cmp xs) (sortBy cmp ys) == sortBy cmp (xs ++ ys) where types = xs :: [ (Int, Int) ] cmp (x1,_) (x2,_) = compare x1 x2

Returns true if the given list contains any of the elements in the search list.

takeWhileList :: ([a] -> Bool) -> [a] -> [a]Source

Similar to Data.List.takeWhile, takes elements while the func is true. The function is given the remainder of the list to examine.

dropWhileList :: ([a] -> Bool) -> [a] -> [a]Source

Similar to Data.List.dropWhile, drops elements while the func is true. The function is given the remainder of the list to examine.

spanList :: ([a] -> Bool) -> [a] -> ([a], [a])Source

Similar to Data.List.span, but performs the test on the entire remaining list instead of just one element.

`spanList p xs`

is the same as `(takeWhileList p xs, dropWhileList p xs)`

keysAL :: [(key, a)] -> [key]Source

Returns the keys that comprise the (key, value) pairs of the given AL.

Same as:

map fst

valuesAL :: [(a, value)] -> [value]Source

Returns the values the comprise the (key, value) pairs of the given AL.

Same as:

map snd

breakList :: ([a] -> Bool) -> [a] -> ([a], [a])Source

Similar to Data.List.break, but performs the test on the entire remaining list instead of just one element.

replace :: Eq a => [a] -> [a] -> [a] -> [a]Source

Given a list and a replacement list, replaces each occurance of the search list with the replacement list in the operation list.

Example:

replace "," "." "127,0,0,1" -> "127.0.0.1"

This could logically be thought of as:

replace old new l = join new . split old $ l

genericJoin :: Show a => String -> [a] -> StringSource

Like `intercalate`

, but works with a list of anything showable, converting
it to a String.

Examples:

genericJoin ", " [1, 2, 3, 4] -> "1, 2, 3, 4" genericJoin "|" ["foo", "bar", "baz"] -> "\"foo\"|\"bar\"|\"baz\""

addToAL :: Eq key => [(key, elt)] -> key -> elt -> [(key, elt)]Source

Adds the specified (key, value) pair to the given list, removing any existing pair with the same key already present.

delFromAL :: Eq key => [(key, a)] -> key -> [(key, a)]Source

Removes all (key, value) pairs from the given list where the key matches the given one.

flipAL :: (Eq key, Eq val) => [(key, val)] -> [(val, [key])]Source

Flips an association list. Converts (key1, val), (key2, val) pairs to (val, [key1, key2]).

strFromAL :: (Show a, Show b) => [(a, b)] -> StringSource

Converts an association list to a string. The string will have one pair per line, with the key and value both represented as a Haskell string.

This function is designed to work with [(String, String)] association lists, but may work with other types as well.

countElem :: Eq a => a -> [a] -> IntSource

Returns a count of the number of times the given element occured in the given list.

elemRIndex :: Eq a => a -> [a] -> Maybe IntSource

Returns the rightmost index of the given element in the given list.

alwaysElemRIndex :: Eq a => a -> [a] -> IntSource

Like elemRIndex, but returns -1 if there is nothing found.