Agda-2.5.2: A dependently typed functional programming language and proof assistant

Safe Haskell | None
Language | Haskell2010

Agda.Utils.List

Description

Utitlity functions on lists.

Synopsis

- caseList :: [a] -> b -> (a -> [a] -> b) -> b
- listCase :: b -> (a -> [a] -> b) -> [a] -> b
- headMaybe :: [a] -> Maybe a
- headWithDefault :: a -> [a] -> a
- lastMaybe :: [a] -> Maybe a
- uncons :: [a] -> Maybe (a, [a])
- mcons :: Maybe a -> [a] -> [a]
- initLast :: [a] -> Maybe ([a], a)
- (!!!) :: [a] -> Int -> Maybe a
- downFrom :: Integral a => a -> [a]
- updateHead :: (a -> a) -> [a] -> [a]
- updateLast :: (a -> a) -> [a] -> [a]
- updateAt :: Int -> (a -> a) -> [a] -> [a]
- mapEither :: (a -> Either b c) -> [a] -> ([b], [c])
- deal :: (a -> Either b c) -> a -> ([b], [c]) -> ([b], [c])
- takeWhileJust :: (a -> Maybe b) -> [a] -> [b]
- spanJust :: (a -> Maybe b) -> [a] -> ([b], [a])
- partitionMaybe :: (a -> Maybe b) -> [a] -> ([a], [b])
- filterAndRest :: (a -> Bool) -> [a] -> ([a], [a])
- mapMaybeAndRest :: (a -> Maybe b) -> [a] -> ([b], [a])
- dropCommon :: [a] -> [b] -> ([a], [b])
- isSublistOf :: Eq a => [a] -> [a] -> Bool
- type Prefix a = [a]
- type Suffix a = [a]
- stripPrefixBy :: (a -> a -> Bool) -> Prefix a -> [a] -> Maybe (Suffix a)
- data PreOrSuffix a
  - = IsPrefix a [a]
  - | IsSuffix a [a]
  - | IsBothfix
  - | IsNofix
- preOrSuffix :: Eq a => [a] -> [a] -> PreOrSuffix a
- wordsBy :: (a -> Bool) -> [a] -> [[a]]
- chop :: Int -> [a] -> [[a]]
- chopWhen :: (a -> Bool) -> [a] -> [[a]]
- holes :: [a] -> [(a, [a])]
- sorted :: Ord a => [a] -> Bool
- distinct :: Eq a => [a] -> Bool
- fastDistinct :: Ord a => [a] -> Bool
- allEqual :: Eq a => [a] -> Bool
- duplicates :: Ord a => [a] -> [a]
- groupBy' :: (a -> a -> Bool) -> [a] -> [[a]]
- groupOn :: Ord b => (a -> b) -> [a] -> [[a]]
- splitExactlyAt :: Integral n => n -> [a] -> Maybe ([a], [a])
- genericElemIndex :: (Eq a, Integral i) => a -> [a] -> Maybe i
- zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
- nubOn :: Ord b => (a -> b) -> [a] -> [a]
- uniqOn :: Ord b => (a -> b) -> [a] -> [a]
- commonSuffix :: Eq a => [a] -> [a] -> [a]
- commonPrefix :: Eq a => [a] -> [a] -> [a]
- editDistanceSpec :: Eq a => [a] -> [a] -> Int
- editDistance :: Eq a => [a] -> [a] -> Int

# Documentation

caseList :: [a] -> b -> (a -> [a] -> b) -> b #

Case distinction for lists, with list first. Cf. `ifNull`.

listCase :: b -> (a -> [a] -> b) -> [a] -> b #

Case distinction for lists, with list last.

headMaybe :: [a] -> Maybe a #

Head function (safe).

headWithDefault :: a -> [a] -> a #

Head function (safe). Returns a value on empty lists.

    headWithDefault 42 []      = 42
    headWithDefault 42 [1,2,3] = 1

lastMaybe :: [a] -> Maybe a #

Last element (safe).

uncons :: [a] -> Maybe (a, [a]) #

Opposite of cons `(:)`, safe.

mcons :: Maybe a -> [a] -> [a] #

Maybe cons. `mcons ma as = maybeToList ma ++ as`

initLast :: [a] -> Maybe ([a], a) #

`init` and `last` in one go, safe.

(!!!) :: [a] -> Int -> Maybe a #

Lookup function (partially safe).

downFrom :: Integral a => a -> [a] #

downFrom n = [n-1,..1,0]

updateHead :: (a -> a) -> [a] -> [a] #

Update the first element of a list, if it exists.

updateLast :: (a -> a) -> [a] -> [a] #

Update the last element of a list, if it exists.

updateAt :: Int -> (a -> a) -> [a] -> [a] #

Update nth element of a list, if it exists. Precondition: the index is >= 0.

mapEither :: (a -> Either b c) -> [a] -> ([b], [c]) #

A generalized version of `partition`. (Cf. `mapMaybe` vs. `filter`).

deal :: (a -> Either b c) -> a -> ([b], [c]) -> ([b], [c]) #

takeWhileJust :: (a -> Maybe b) -> [a] -> [b] #

A generalized version of `takeWhile`. (Cf. `mapMaybe` vs. `filter`).

spanJust :: (a -> Maybe b) -> [a] -> ([b], [a]) #

A generalized version of `span`.

partitionMaybe :: (a -> Maybe b) -> [a] -> ([a], [b]) #

Partition a list into `Nothing`s and `Just`s. ``mapMaybe` f = snd . partitionMaybe f`.

filterAndRest :: (a -> Bool) -> [a] -> ([a], [a]) #

Like `filter`, but additionally return the last partition of the list where the predicate is `False` everywhere.

mapMaybeAndRest :: (a -> Maybe b) -> [a] -> ([b], [a]) #

Like `mapMaybe`, but additionally return the last partition of the list where the function always returns `Nothing`.

dropCommon :: [a] -> [b] -> ([a], [b]) #

Drops from both lists simultaneously until one list is empty.

isSublistOf :: Eq a => [a] -> [a] -> Bool #

Sublist relation.

type Prefix a = [a] #

type Suffix a = [a] #

stripPrefixBy :: (a -> a -> Bool) -> Prefix a -> [a] -> Maybe (Suffix a) #

Check if a list has a given prefix. If so, return the list minus the prefix.

data PreOrSuffix a #

Result of `preOrSuffix`.

Constructors

IsPrefix a [a] | First list is prefix of second.
IsSuffix a [a] | First list is suffix of second.
IsBothfix | The lists are equal.
IsNofix | The lists are incomparable.

preOrSuffix :: Eq a => [a] -> [a] -> PreOrSuffix a #

Compare lists with respect to prefix partial order.

wordsBy :: (a -> Bool) -> [a] -> [[a]] #

Split a list into sublists. Generalisation of the prelude function `words`.

    words xs == wordsBy isSpace xs

chop :: Int -> [a] -> [[a]] #

Chop up a list in chunks of a given length.

chopWhen :: (a -> Bool) -> [a] -> [[a]] #

Chop a list at the positions when the predicate holds. Contrary to `wordsBy`, consecutive separator elements will result in an empty segment in the result. > intercalate [x] (chopWhen (== x) xs) == xs

holes :: [a] -> [(a, [a])] #

All ways of removing one element from a list.

sorted :: Ord a => [a] -> Bool #

Check whether a list is sorted.

Assumes that the `Ord` instance implements a partial order.

distinct :: Eq a => [a] -> Bool #

Check whether all elements in a list are distinct from each other. Assumes that the `Eq` instance stands for an equivalence relation.

fastDistinct :: Ord a => [a] -> Bool #

An optimised version of `distinct`.

Precondition: The list's length must fit in an `Int`.

allEqual :: Eq a => [a] -> Bool #

Checks if all the elements in the list are equal. Assumes that the `Eq` instance stands for an equivalence relation.

duplicates :: Ord a => [a] -> [a] #

Returns an (arbitrary) representative for each list element that occurs more than once.

groupBy' :: (a -> a -> Bool) -> [a] -> [[a]] #

A variant of `groupBy` which applies the predicate to consecutive pairs.

groupOn :: Ord b => (a -> b) -> [a] -> [[a]] #

``groupOn` f = `groupBy` ((`==`) `on` f) `.` `sortBy` (`compare` `on` f)`.

splitExactlyAt :: Integral n => n -> [a] -> Maybe ([a], [a]) #

`splitExactlyAt n xs = Just (ys, zs)` iff `xs = ys ++ zs` and `genericLength ys = n`.

genericElemIndex :: (Eq a, Integral i) => a -> [a] -> Maybe i #

A generalised variant of `elemIndex`.

zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c] #

Requires both lists to have the same length.

nubOn :: Ord b => (a -> b) -> [a] -> [a] #

Efficient variant of `nubBy` for finite lists.

Specification:

    nubOn f xs == 'nubBy' ((==) `'on'` f) xs.

uniqOn :: Ord b => (a -> b) -> [a] -> [a] #

Efficient variant of `nubBy` for finite lists.

Specification: For each list `xs` there is a list `ys` which is a permutation of `xs` such that

    uniqOn f xs == 'nubBy' ((==) `'on'` f) ys.

Furthermore

    sortBy (compare `on` f) (uniqOn f xs) == uniqOn f xs.

commonSuffix :: Eq a => [a] -> [a] -> [a] #

Compute the common suffix of two lists.

commonPrefix :: Eq a => [a] -> [a] -> [a] #

Compute the common prefix of two lists.

editDistanceSpec :: Eq a => [a] -> [a] -> Int #

editDistance :: Eq a => [a] -> [a] -> Int #