LISP - notevery function
Lisp - The notevery Function
Section titled “Lisp - The notevery Function”The notevery function is a sequence predicate that tests if at least one element in a sequence fails to satisfy a given test. It is the logical negation of the every function.
Think of it this way: (every predicate sequence) asks, “Does every element satisfy the predicate?” while (notevery predicate sequence) asks, “Is there at least one element that does not satisfy the predicate?”.
Logically, (notevery p s) is equivalent to (not (every p s)).
Syntax: notevery
Section titled “Syntax: notevery”(notevery predicate &rest sequences+)Parameters
Section titled “Parameters”- predicate: A function that accepts as many arguments as there are sequences and returns a boolean value (
tornil). - sequences+: One or more sequences (lists, vectors, or strings) to be tested.
Return Value
Section titled “Return Value”It returns t if the predicate returns nil for at least one element (or set of elements). It returns nil only if the predicate returns t for all elements. The function short-circuits, meaning it stops and returns t as soon as it finds the first element that fails the test.
Example: Checking if a List Contains an Odd Number
Section titled “Example: Checking if a List Contains an Odd Number”Here, notevery with the predicate #'evenp checks if there is any number in the sequence that is not even.
main.lisp
Section titled “main.lisp”;;; Test a list of only even numbers(format t "Is there an odd number in '(2 4 6 8)? ~a~%" (notevery #'evenp '(2 4 6 8))) ; -> NIL, because all are even.
;; Test a vector of only even numbers(format t "Is there an odd number in '#(2 4 6 8)? ~a~%" (notevery #'evenp '#(2 4 6 8))) ; -> NIL, because all are even.
;; Test a list containing an odd number(format t "Is there an odd number in '(2 4 9 8)? ~a~%" (notevery #'evenp '(2 4 9 8))) ; -> T, because 9 is not even.Output
Section titled “Output”Is there an odd number in '(2 4 6 8)? NILIs there an odd number in '#(2 4 6 8)? NILIs there an odd number in '(2 4 9 8)? TExample: Using a Lambda Predicate
Section titled “Example: Using a Lambda Predicate”You can use a lambda function for more complex, custom predicates. Let’s check if any number in a list is 10 or less.
main.lisp
Section titled “main.lisp”(let ((predicate #'(lambda (x) (> x 10)))) ;; Test a list where all numbers are > 10 (format t "Is any number in '(11 15 14) not > 10? ~a~%" (notevery predicate '(11 15 14))) ; -> NIL
;; Test a list where some numbers are not > 10 (format t "Is any number in '(11 5 14) not > 10? ~a~%" (notevery predicate '(11 5 14)))); -> TOutput
Section titled “Output”Is any number in '(11 15 14) not > 10? NILIs any number in '(11 5 14) not > 10? TExample: Checking a String for Non-Alphabetic Characters
Section titled “Example: Checking a String for Non-Alphabetic Characters”notevery works on strings as well. We can check if a string contains any non-alphabetic characters.
main.lisp
Section titled “main.lisp”;; Check a purely alphabetic string(format t "Does 'tutorialspoint' contain any non-alphabetic characters? ~a~%" (notevery #'alpha-char-p "tutorialspoint")) ; -> NIL
;; Check a string with digits(format t "Does 'lisp-101' contain any non-alphabetic characters? ~a~%" (notevery #'alpha-char-p "lisp-101")) ; -> T (stops at '-')Output
Section titled “Output”Does 'tutorialspoint' contain any non-alphabetic characters? NILDoes 'lisp-101' contain any non-alphabetic characters? TExample: Working with Multiple Sequences
Section titled “Example: Working with Multiple Sequences”When given multiple sequences, the predicate receives one element from each sequence at each step. notevery will test if the predicate is ever false for any corresponding element pair.
main.lisp
Section titled “main.lisp”;; Check if any element in the first list is NOT less than the corresponding element in the second.(format t "Is (notevery #'< '(1 2 3) '(4 5 6)) -> ~a~%" (notevery #'< '(1 2 3) '(4 5 6))) ; -> NIL, because 1<4, 2<5, and 3<6 are all true.
;; Check again, but with one failing case.(format t "Is (notevery #'< '(1 8 3) '(4 5 6)) -> ~a~%" (notevery #'< '(1 8 3) '(4 5 6))) ; -> T, because (not (< 8 5)) is true.Output
Section titled “Output”Is (notevery #'< '(1 2 3) '(4 5 6)) -> NILIs (notevery #'< '(1 8 3) '(4 5 6)) -> TPractical Applications
Section titled “Practical Applications”notevery is particularly useful for validation and checks where you are looking for a single failure or invalid item.
- Data Validation: To check if any submitted form field is invalid.
(notevery #'valid-field-p form-data)would returnTif there’s at least one bad field. - Security: To confirm that a password contains at least one non-alphanumeric character.
(notevery #'alphanumericp "p@ssword1")returnsT, confirming the password meets the complexity rule. - Quality Control: To find if any item in a production batch fails a quality check.
(notevery #'quality-passed-p batch-items)returnsTif a defect is found.