list-predicate-0.1.0.1: Predicates on lists

Safe HaskellSafe
LanguageHaskell2010

Data.List.Predicate

Contents

Description

Predicates (True/False queries) on lists.

The functions in this module are as lazy as possible. For example, sortedBy undefined [undefined] == True, since a list of one element must be sorted, no matter the comparison function, or the value of the element.

Synopsis

All equal

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

O(n). Whether the elements are all equal.

>>> allEqual [1..]
False
>>> allEqual [3, 3, 3, 3]
True
>>> allEqual []
True
>>> allEqual [1]
True

allEqualBy :: (a -> a -> Bool) -> [a] -> Bool Source #

O(n). Like allEqual, with a custom equality test.

>>> allEqualBy ((==) `on` (`mod` 10)) [3, 13, 23]
True
>>> allEqualBy ((==) `on` (`mod` 10)) [3, 13, 24]
False

Sortedness

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

O(n). Whether the elements are in sorted order.

>>> sorted [1, 2, 3, 3]
True
>>> sorted [1, 2, 3, 2]
False
>>> sorted []
True
>>> sorted [1]
True

sortedBy :: (a -> a -> Ordering) -> [a] -> Bool Source #

O(n). Like sorted, with a custom comparison test.

>>> sortedBy (comparing Down) [3, 2, 1]
True
>>> sortedBy (comparing Down) [3, 2, 1, 2]
False

All unique

allUnique :: Ord a => [a] -> Bool Source #

O(n log(n)). Whether the elements are all unique.

>>> allUnique [1, 2, 5, 7]
True
>>> allUnique [1, 2, 5, 2]
False
>>> allUnique []
True
>>> allUnique [1]
True

allUniqueBy :: (a -> a -> Ordering) -> [a] -> Bool Source #

O(n log(n)). Like allUnique, with a custom comparison test.

>>> allUniqueBy (comparing head) ["apple", "bow", "cat"]
True
>>> allUniqueBy (comparing head) ["apple", "bow", "ant"]
False

allAdjUnique :: Eq a => [a] -> Bool Source #

O(n). Whether all adjacent pairs of elements are different. Useful for determining whether a sorted list is all unique.

>>> allAdjUnique [1, 2, 3, 2]
True
>>> allAdjUnique [1, 2, 2, 3]
False
>>> allAdjUnique []
True
>>> allAdjUnique [1]
True

allAdjUniqueBy :: (a -> a -> Bool) -> [a] -> Bool Source #

O(n). Like allAdjUnique, with a custom equality test.

>>> allAdjUniqueBy ((==) `on` head) ["apple", "bow", "cat", "ant"]
True
>>> allAdjUniqueBy ((==) `on` head) ["apple", "ant", "bow", "cat"]
False

Sequential

ascSequential :: Enum a => [a] -> Bool Source #

O(n). Whether the list is increasing sequentially (one-by-one).

>>> ascSequential [1, 2, 3, 4, 5]
True
>>> ascSequential [1, 2, 3, 4, 8]
False
>>> ascSequential ([] :: [Int])
True
>>> ascSequential [1]
True

descSequential :: Enum a => [a] -> Bool Source #

O(n). Whether the list is descending sequentially (one-by-one).

>>> descSequential [5, 4, 3, 2, 1]
True
>>> descSequential [5, 4, 3, 3, 1]
False
>>> descSequential ([] :: [Int])
True
>>> descSequential [1]
True

Miscellaneous

palindrome :: Eq a => [a] -> Bool Source #

O(n). Whether the input is a palindrome, i.e., the same forwards and backwards.

>>> palindrome "rotor"
True
>>> palindrome "rover"
False
>>> palindrome ""
True
>>> palindrome "a"
True