LISP - notany function
Lisp - The notany Function
Section titled “Lisp - The notany Function”The notany function is a sequence predicate that determines if a given test is false for every element in a sequence. It is the logical negation of the some function. You can think of notany as asking the question: “Is there no element that satisfies this condition?”
Syntax
Section titled “Syntax”(notany predicate sequence &rest more-sequences)Arguments
Section titled “Arguments”- predicate: A function designator (e.g., a function name or a lambda function) that accepts one argument per sequence.
- sequence: The primary sequence to test. This can be a list, a vector, or a string.
- more-sequences: Optional additional sequences. If provided, the predicate must accept a number of arguments equal to the total number of sequences.
Return Value
Section titled “Return Value”Returns T (true) if the predicate returns NIL (false) for every element. If the predicate returns a non-NIL value for any element, notany immediately stops and returns NIL.
Example 1: Basic Check for Even Numbers
Section titled “Example 1: Basic Check for Even Numbers”Let’s check if a list contains any even numbers.
;; The predicate is #'evenp.;; Is it true that NO element is even?
;; Returns T because no number in the list is even.(notany #'evenp '(1 3 5 7 9)); => T
;; Returns NIL because 2 is even, so the condition "no element is even" is false.(notany #'evenp '(1 2 3 5 7 9)); => NILExample 2: Using a lambda for a Custom Predicate
Section titled “Example 2: Using a lambda for a Custom Predicate”We can use an anonymous lambda function to perform custom checks, such as finding if any number is greater than 10.
;; Predicate: a lambda checking if a number is greater than 10.(defvar *my-numbers* '(1 15 4 2))
;; Is it true that NO number is greater than 10?;; Returns NIL because 15 > 10.(notany #'(lambda (x) (> x 10)) *my-numbers*); => NIL
;; Is it true that NO number is greater than 20?;; Returns T because all numbers are <= 20.(notany #'(lambda (x) (> x 20)) *my-numbers*); => TExample 3: Working with Strings
Section titled “Example 3: Working with Strings”Sequences can also be strings. Here, we check if a string contains any numeric digits.
;; The predicate #'digit-char-p checks if a character is a digit.
;; Is it true that NO character is a digit?;; Returns T because the string contains only letters.(notany #'digit-char-p "TutorialsPoint"); => T
;; Returns NIL because '1' is a digit.(notany #'digit-char-p "Lisp-101"); => NILExample 4: Using Multiple Sequences
Section titled “Example 4: Using Multiple Sequences”When using multiple sequences, the predicate must accept one argument from each sequence.
;; Predicate: #'> (greater than);; Checks if any element from the first list is greater than the;; corresponding element from the second list.
;; Is it true that NO element from list1 is greater than its counterpart in list2?;; Returns NIL because 3 > 2.(notany #'> '(1 2 3) '(4 1 2)); => NIL
;; Returns T because in every pair (1 4), (2 5), (3 6), the first element is not greater.(notany #'> '(1 2 3) '(4 5 6)); => TCommon Applications and Debugging
Section titled “Common Applications and Debugging”- Data Validation: Quickly verify that a collection of inputs contains no invalid values (e.g.,
(notany #'is-invalid-p user-inputs)). - State Verification: Ensure a system is in a safe state by checking that no component has an error flag (e.g.,
(notany #'has-error-p *system-components*)). - Set Operations: Check if two sets are disjoint (have no elements in common).
- Debugging Tip: Remember that
notanyon an empty sequence()will always returnT, because the condition “the predicate is true for an element” is never met.