← All languages
Haskell function of the day
Random

any

Test if any element satisfies a predicate.

Description

any takes a predicate and a Foldable structure, returning True if at least one element satisfies the predicate. Its type signature is Foldable t => (a -> Bool) -> t a -> Bool. If the structure is empty, any returns False, since no element can satisfy the predicate.

any short-circuits: it stops traversing as soon as it finds an element that satisfies the predicate. This makes it efficient for large or even infinite structures when a matching element exists early. For example, any even [1..] returns True after checking only the first two elements.

any is the existential quantifier over a structure. It pairs with all, which is the universal quantifier. Common uses include checking for error conditions, validating input, and searching for specific properties. The expression any (== x) xs is equivalent to elem x xs.

Arguments

NameDescriptionOptional
p A predicate function (a -> Bool) to test each element. No
xs A Foldable structure to search through. No

Example

any even [1, 3, 5, 4]   -- True
any even [1, 3, 5]      -- False
any (> 10) [1..]         -- True

Reference