lists-0.4.2: Functions for dealing with lists

Safe Haskell Safe-Inferred

Data.Lists

Contents

Description

Functions for dealing with lists.

Synopsis

module Data.List

# List operations

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

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

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

Get the union of the given lists.

for :: [a] -> (a -> b) -> [b]Source

Opposite of map.

lastToMaybe :: [a] -> Maybe aSource

Maybe get the last element in the list.

firstOr :: a -> [a] -> aSource

Return the first item of a list or something else.

maxList :: (Num t, Ord t) => [t] -> tSource

Get the maximum of a list or return zero.

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

Arguments

 :: Eq a => [a] List of elements to look for -> [a] List to search -> Bool Result

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.

hasKeyAL :: Eq a => a -> [(a, b)] -> BoolSource

Indicates whether or not the given key is in the AL.

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

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

strToAL :: (Read a, Read b) => String -> [(a, b)]Source

The inverse of `strFromAL`, this function reads a string and outputs the appropriate association list.

Like `strFromAL`, this is designed to work with [(String, String)] association lists but may also work with other objects with simple representations.

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.

seqList :: [a] -> [a]Source

Forces the evaluation of the entire list.